skip main navigation skip all navigation top
 

setAttribute workaround for IE

 

Another beautiful quirk in Internet Explorer is it's poor implementation of the setAttribute method. It's something I tried to make use of for my custom 'lightbox', but off course I had a lot of trouble getting it working.

After a bit of searching I came across a fix at webmasterworld.com. Using the setAttribute method should allow you to apply a class, an id, an event or any other attribute to an element. From what I've read, IE does this to some extent. It does apply the attribute you request, but not to the HTML element, it instead applies it to the JavaScript reference to the HTML element.

A subtle difference in definition, but another subtle difference that can add hours to your development time!

Whereas compliant browsers would use:

element.setAttribute('onclick','event();')



IE needs the following:

element.onclick = eventCaller;

function eventCaller() {
  event();
}



Once I had the above working, I was confident that I could work around this so that the eventCaller function wasn't necessary, but it seems that it is.

For other attributes, such as the class name, you'll need the following:

element.className = 'class-name';



Styles work in a different way and it makes sense not to use the setAttribute method, but it would be nice to have the option to apply all your styles just once, rather than creating a new reference for every single one.

Microsoft promises that IE8 will be the most compliant version, but I sincerely hope that their claims don't just apply to CSS and the DOM, but to the JavaScript API and JavaScript methods too.

  • Digg
  • del.icio.us
  • Reddit
  • Technorati
  • YahooMyWeb
  • StumbleUpon
  • SphereIt
  • Furl
  • Google
  • Facebook
  • Live

This post has the following tags: IE, JavaScript, setAttribute, web development

This post was filed under the following categories: Code

First posted: 10th Jul 2008 @ 02:01
This post has been updated on 1 occassion(s):
Last updated: 10th Jul 2008 @ 02:18

Further reading...

Here are some articles that you might find interesting.

The post setAttribute workaround for IE by Charanjit Chana is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.0 UK: England & Wales License. Permissions beyond the scope of this license may be available by getting in contact with the author.

Charanjit Chana BSc

My very own blog that covers a wide range of topics. I'm a web developer by day, and by night, behind the projects Nike+PHP and receeve.it

Search

Categories

Tags

See all tags

Social networks

Nike+

Links

Advertisements