SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Pedantic Semantic blain's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Best way to set element class

    Just wondering
    Code JavaScript:
    Link.setAttribute('class', 'foo')
    and
    Code JavaScript:
    Link.className='foo';
    both work, which is the better usage and why?
    Technology is dominated by two types of people:
    those who understand what they do not manage,
    and those who manage what they do not understand.

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tend to use the second form because IE seems to have issues sometimes with setAttribute. In a realistic world though, setAttribute is the DOM standard and should be used.

  3. #3
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The latter, as chris said it's compatible with IE. But in the real world I would use a custom function to add/change a class because you never know if an element already has a class or not, and setting a class can replace a class that's there.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •