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?
Click Options > Under the Hood. In the Privacy section, click Content settings. Scroll to the JavaScript section and click Allow all sites to run JavaScript (recommended). Close the Options tab and refresh the browser.
JavaScript is a programming language used to make web pages interactive. Like HTML and CSS, you do not need to install anything to begin writing JavaScript or see it run on your computer. All modern browsers support JavaScript.
You can reliably use the className
property instead of setAttribute
:
document.getElementById("gifts").className = "gkvSprite selected";
More generally, there are a couple of attribute names that different browsers treat differently in setAttribute
: class
vs className
, and for
vs. htmlFor
. Libraries like Prototype, jQuery, and the like will smooth out these differences for you, although (again) in the specific situation of class
, you can reliably use the property instead.
You can go about this in a few ways.
If you want to use setAttribute
you can detect which browser the client is using and then use class
in IE and classname
in Firefox.
The above would work but I would prefer using a div and assigning a new class for that.
somediv.className='gkvSprite selected'
Or as T.J. Crowder said above. Asign via Classname
directily.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With