Home » Programming / Coding

setAttribute does not work in IE

27 March 2010 2,797 views One Comment

Life ain’t so smooth for coders often.  Often times developing websites making them cross-browser compatible makes life a nightmare.

Try this code:

document.getElementById(“myElementId”).setAttribute(“class”, “myclass”);

This code works fine in Mozilla and many other major browsers. But hey once again our friend Internet Explorer doesn’t work with it.

There are several solutions to work with this. You can assign individual style elements seperately. For example:

document.getElementById(“myElementId”).style.color=’#000′;

However problem is not over yet. You cant assign all the style attributes. For example if you want make a text italic, this assignment wont work in Google Chrome.

document.getElementById(“myElementId”).style.fontStyle=’italic’;

Cross Browser Solution?

This solution seem to work on IE 7, IE 8, Mozilla, Chrome, Safari.

document.getElementById(“myElementId”).className=’searchBox2′;

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

One Comment »

  • itai said:

    thank you! this solves everything was trying to figure this out for an hour.

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.