wcndave
wcndave

Reputation: 314

Jquery append subtleties in IE8 and below

I have the following code:

var editLink = $('<a href=# class="edit" id="edit">').append('edit')

It doesn't work in IE8 and below, which creates nothing at all when I use editLink later on.

This does work:

var editLink = $('<a href=# class="edit" id="edit">edit</a>')

However I have a lot of nested attributes to put in, and don't really want a very long piece of HTML in the middle of the jQuery.

Any ideas why the first version doesn't work?

Upvotes: 0

Views: 23

Answers (1)

wcndave
wcndave

Reputation: 314

I found the answer.

It's simply that the element being created needs to be closed. ie

var editLink = $('<a href=# class="edit" id="edit"></a>').append('edit');

I had been worried that this would append the text after the element, rather than inside it, however of course append means add to the end of the inside of the targeted element.

So this works perfectly. With fields that don't have closing tags, like,

<input type=text> 

and so on, you don't need to have the closing element.

Upvotes: 2

Related Questions