Lesson learnt with creating DOM element with jQuery
04 April 2008
This took me some seriously wasted time to figure out yesterday. What I was trying to do was to create a DOM element of tag type
A and insert it into the DOM tree of my page. As I was coding along, everything was working just fine in Firefox but the damn thing wouldn't show up anywhere in IE 6. I debugged and debugged and tried all kinds of different approaches and I just couldn't work it out. Then Karl Rudd gave the right hint on the jQuery mailing list.
Basically, what I was doing was something like this:
var a = $("<a>").attr('href','#').click(somefunction); $('#toolbar').append(a);
What was then so strange is now less surprising. When I changed the
<a> to a
<span> it actually worked but just looked wrong with the rest of the site I was working on. Here's the correct way of doing it:
var a = $("<a></a>").attr('href','#').click(somefunction); $('#toolbar').append(a);
Notice the difference between
$("<div>") which was working fine. I'll have to get back to figuring out why that one worked nad the
A one didn't.