Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 894731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:26:35+00:00 2026-05-15T14:26:35+00:00

how to set element attribute with javascript in IE6..? It seems setAttribute doesn’t work.

  • 0

how to set element attribute with javascript in IE6..? It seems setAttribute doesn’t work. I really need to do it on the fly. Thanks.

Code

<script type="text/javascript" language="javascript"> 
    menuItems = document.getElementById("menu").childNodes; 
    for (i = 0; i < menuItems.length; i++)
    { 
        if (menuItems[i].className != "blue") 
            menuItems[i].setAttribute('onmouseover', 'HoverMenu(this)'); 
    } 
</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:26:36+00:00Added an answer on May 15, 2026 at 2:26 pm

    (Most of the below was before the OP clarified they were setting an event handler; left the list of other issues in case others find them useful)

    IE6 makes a mess of several aspects of setAttribute. Without knowing the exact problem you were dealing with (this was before the edit inidicating it was an event handler), it’s hard to be sure whether that’s really the problem, but here are a couple of known issues:

    You can’t use setAttribute to set event handlers

    It’s best to set event handlers using the reflected property or with addEventListener [standard] / attachEvent [IE], not setAttribute (and you can’t use setAttribute on IE).

    So, any of these will work:

    // Using reflected property
    theElement.onclick = yourFunction;
    
    // Using DOM2 standard addEventListener; note it's "click", not "onclick"
    theElement.addEventListener("click", yourFunction, false);
    
    // IE's non-standard alternative to addEventListener
    theElement.attachEvent("onclick", yourFunction);
    

    …not

    // This doesn't work on IE (at least)
    theElement.setAttribute("onclick", "yourFunction();");
    

    The addEventListener / attachEvent way of doing this is cool for other reasons: You can have multiple event listeners on the same event of an element. You can’t do that using the reflected property.

    So for your specific situation:

    menuItems = document.getElementById("menu").childNodes; 
    for (i = 0; i < menuItems.length; i++)
    { 
        if (menuItems[i].className != "blue") {
            menuItems[i].onmouseover = function() {
                HoverMenu(this);
            }
        }
    }
    

    Certain attributes need their modified names

    class

    The correct way to set the class attribute is to use the reflected property className:

    // Correct, cross-browser way. Works on IE6+, all versions of
    // Chrome, etc. Completely reliable.
    theElement.className = "new set of classes";
    

    or on modern browsers (so, not IE6!) you can use classList.

    One of IE6’s many setAttribute bugs is that this doesn’t work:

    // Should also work, but fails on IE6 (and probably some other old versions)
    theElement.setAttribute("class", "new set of classes");
    

    Instead, IE6 (and probably a couple of other versions) make you use "className" instead of "class", even though that makes no sense whatsoever. The reflected property has the name className because it used to be that in JavaScript, you couldn’t use reserved words (like class or for or if) as property literals (obj.class was invalid). That’s not been true for a while now (ECMAScript 5 fixed it), but that’s why the reflected property is className, not class.

    But since setAttribute takes a string, it should accept the proper name of the attribute. The fact it doesn’t is just an IE bug (and one they’ve fixed in modern versions of IE if they’re not in [in]compatibility mode).

    for

    Similarly, to set the for attribute (for instance, on labels), one uses the htmlFor reflected property:

    // Correct, cross-browser way. Works on IE6+, all versions of
    // Chrome, etc. Completely reliable.
    theLabel.htmlFor = "id-of-field";
    

    On any non-broken browser, you can also use setAttribute:

    // Should also work, but fails on IE6 (and probably some other old versions)
    theLabel.setAttribute("for", "id-of-field");
    

    …but not on IE6, it wants "htmlFor" instead of "for" for the same reason it wants "className" rather than "class" (e.g, because it’s broken).

    This page has quite a list of attributes that are problematic with IE.

    setAttribute can’t be used to set the style attribute

    …you have to use the style property instead; but to be fair, that’s usually a more convenient way. Example: This won’t work on IE:

    theElement.setAttribute("style", "color: blue"); // Doesn't work on IE
    

    …but this will:

    myElement.style.color = "blue";
    

    Slightly OT: Look at libraries

    JavaScript libraries like Prototype, jQuery, Closure, or any of several others will make most of the above a lot easier and smooth out differences amongst browsers if you go through their APIs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 534k
  • Answers 534k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try setting a global variable prior to utilizing the class.… May 17, 2026 at 12:48 am
  • Editorial Team
    Editorial Team added an answer Your description is pretty confusing, but do you mean something… May 17, 2026 at 12:48 am
  • Editorial Team
    Editorial Team added an answer No, you can't. In Scala, constructor parameters are properties, so… May 17, 2026 at 12:48 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Usually, if you need to set a style attribute in JavaScript, you say something
Styling an element with an attribute set is easy: E[attr] . Is there any
I want to set an attribute of a DOM element using element[attribute] , but
I thought that adding a value attribute set on the <select> element below would
I'm trying to set a css expression for an attribute for an element as
I have a master page with a form element and the defaultbutton attribute set
I want to associate a JavaScript object with an HTML element. Is there a
I would like to get the value of the class attribute for an element
I had posted one question earlier jQuery inconsistency in setting readonly attribute in IE-8
My code is meant to replace radio buttons with dynamic ones, and allow clicking

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.