Results 1 to 3 of 3

Thread: IE Mystery with className

  1. #1
    Join Date
    Jan 2010

    IE Mystery with className

    I have a Web page that runs properly with IE 9 with the following code:

    // Check for IE
       if (isBrowserIE) tempArray[i].setAttribute("className", "red");
       else tempArray[i].setAttribute("class", "red");
    I have now created a new (separate) page that uses the SAME code--I did a copy and paste of the whole function containing this code from the page that works into the new page. However, the function (this if-statement in particular) does not work in the new page--the new page does NOT make the array item red, BUT it does make it red if I change "className" to "class"--just as if IE now uses "class" like everyone else. But then the older page that still has "className" should not work, but it does!

    This would seem to be an impossible scenario. Any ideas?

    Thanks so much.


  2. #2
    Join Date
    Mar 2007
    Never use class as its likely to be a reserved word, and never use setAttribute unless direct assignment fails.

    This should always work:
    tempArray[i].className = 'red';
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Jan 2010
    Thanks, Logic Ali.

    (1) Your more concise code did work, and since it is more concise and direct, I'll probably switch to it. However, my code also worked in IE 9 by simply using "class" rather than "className" in the code segment posted.

    (2) My real question was why my original code worked in IE 9 on one page but did not work on another page on which the whole function containing that code was copy and pasted. The code I posted was identical on the two pages. One page worked (turned the text red) and the other page did not. I was hoping to find an explanation for THAT! As I said in my original post, that would seem to be impossible.


    Last edited by kenmorgan; 10-22-2012 at 12:46 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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