I am using the below JS code in order to change the class when a link is clicked.
document.getElementById("gifts").setAttribute("class", "gkvSprite selected");
This is not working in IE but it does in FF and Chrome. Then I changed the code to:
document.getElementById("gifts").setAttribute("className", "gkvSprite selected");
Then it worked in IE, but stopped working in FF and Chrome.
Could someone please help me out here?
Now that IE is well and truly gone, you can use
setAttribute("class", ___)reliably.Alternatively, you can reliably use the
classNameproperty instead ofsetAttribute:More generally, there are a couple of attribute names that IE (and only IE) got wrong with
setAttribute: IE requiredclassNameinstead ofclass, andhtmlForinstead offor. The reflected property names areclassNameandhtmlFor, but the attribute names areclassandfor, andsetAttributeshould be using the attribute names (and does, except on IE). (IIRC, IE11 fixed this, but it doesn’t matter now that IE has been retired.)