Internet Explorer and setAttribute

Posted on 05/29/2008 @ 03:04PM

While coding some DOM creation funness today, I had the need to dynamically update the css classes of a DOM node. In the process, I realized that IE6 and IE7 won't run the following code properly:

var li = document.createElement("li");
li.setAttribute('class', "class1");

Aparrently this isn't a new discovery. At any rate, I fixed it by using the className property (which is more correct in this case anyway):

var li = document.createElement("li");
li.className = "class1";