How to add/update an attribute to an HTML element using JavaScript?

dev.e.loper picture dev.e.loper · Apr 2, 2009 · Viewed 319.1k times · Source

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.

Answer

andi picture andi · Apr 2, 2009

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.