132

I'm trying to find a way that will add / update attribute using JavaScript. I know I can do it with setAttribute() function but that doesn't work in IE.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
dev.e.loper
  • 35,446
  • 76
  • 161
  • 247

4 Answers4

166

You can read here about the behaviour of attributes in many different browsers, including IE.

element.setAttribute() should do the trick, even in IE. Did you try it? If it doesn't work, then maybe element.attributeName = 'value' might work.

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
andi
  • 14,322
  • 9
  • 47
  • 46
  • 5
    this works. it creates attribute if it doesn't exists and updates it if it does exist. is this documented somewhere as far as how this works? – dev.e.loper Apr 02 '09 at 16:21
  • @dev.e.loper DOM spec is a good place to start: http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-ElSetAttrNS – valentinas Aug 28 '12 at 04:47
  • 2
    When I do the following: `document.getElementById("nav").setAttribute("class", "active");` it works in the Chrome JS console, but in the actual page it doesn't work..any ideas? By the way, in the actual page I include the `.js` file before the end of the `body` scope. – knownasilya Sep 08 '12 at 15:25
36

What seems easy is actually tricky if you want to be completely compatible.

var e = document.createElement('div');

Let's say you have an id of 'div1' to add.

e['id'] = 'div1';
e.id = 'div1';
e.attributes['id'] = 'div1';
e.createAttribute('id','div1')
These will all work except the last in IE 5.5 (which is ancient history at this point but still is XP's default with no updates).

But there are contingencies, of course. Will not work in IE prior to 8:e.attributes['style'] Will not error but won't actually set the class, it must be className:e['class'] .
However, if you're using attributes then this WILL work:e.attributes['class']

In summary, think of attributes as literal and object-oriented.

In literal, you just want it to spit out x='y' and not think about it. This is what attributes, setAttribute, createAttribute is for (except for IE's style exception). But because these are really objects things can get confused.

Since you are going to the trouble of properly creating a DOM element instead of jQuery innerHTML slop, I would treat it like one and stick with the e.className = 'fooClass' and e.id = 'fooID'. This is a design preference, but in this instance trying to treat is as anything other than an object works against you.

It will never backfire on you like the other methods might, just be aware of class being className and style being an object so it's style.width not style="width:50px". Also remember tagName but this is already set by createElement so you shouldn't need to worry about it.

This was longer than I wanted, but CSS manipulation in JS is tricky business.

JPearce
  • 369
  • 3
  • 4
30

Obligatory jQuery solution. Finds and sets the title attribute to foo. Note this selects a single element since I'm doing it by id, but you could easily set the same attribute on a collection by changing the selector.

$('#element').attr( 'title', 'foo' );
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • 3
    +1 for jQuery solution. In my case, I was trying to avoid jQuery and produce it in pure javascript due to the requirements of my work. I'm glad both answers exist. Thanks. – NuclearPeon Feb 01 '13 at 20:22
8

What do you want to do with the attribute? Is it an html attribute or something of your own?

Most of the time you can simply address it as a property: want to set a title on an element? element.title = "foo" will do it.

For your own custom JS attributes the DOM is naturally extensible (aka expando=true), the simple upshot of which is that you can do element.myCustomFlag = foo and subsequently read it without issue.

annakata
  • 74,572
  • 17
  • 113
  • 180