2005-07-21

Internet Explorer's anchor name attribute problem

This week I was working on our content management system. I needed to insert named anchors before specific DOM nodes. Doing so I found out that you are unable to set the name attribute of an anchor in IE. Take a look at the following code:

var div = document.createElement('div');
var a = document.createElement('a');
a.setAttribute('name', 'whatever');
div.appendChild(a);
alert(div.innerHTML);

You would expect an alert saying <a name="whatever"></a>. Execute the above code and see for yourself. I also tried using a.name = 'whatever' which also didn't work.

To meet the deadline I used the onload attribute instead and replaced <a onload by <a name afterwards. Not very pretty. Anyone any better ideas?

No comments: