2

So I have the following div

<div id="object_list"> 

I want to append a list and items into it. So I run the following jquery

 $("#object_list").empty(); $("#object_list").append('<ul'); $("#object_list").append(list.contents()); $("#object_list").append('</ul>'); 

After that code runs, #object_list looks like this

<div id="object_list"> <ul></ul> ...list.contents() elements </div> 

Even after debugging, if I do another $("#object_list").append('<ul>'); all I get is an added <ul> after the </ul>. Why is jquery not appending to the html AFTER the list.contents(), but is going before it?

2
  • Perhaps not as expected, but as documented. jQuery appends a complete element, not literal HTML. Commented May 16, 2010 at 2:56
  • Both answers worked, but could only mark one. Commented May 16, 2010 at 3:09

2 Answers 2

3

The issue is that you keep appending to "object_list" when you are meaning to append items to the list itself. (as an aside, you aren't closing your UL instantiation tag either)

Try something like this:

var $list = $("<ul></ul>"); $list.append("<li>Something 1</li>"); $list.append("<li>Something 2</li>"); $list.append("<li>Something 3</li>"); $("#object_list").append($list); 
Sign up to request clarification or add additional context in comments.

Comments

1

Append doesn't simply append tags, it actually appends elements. In your case you want to append list items to an unordered list that gets appended to your div.

Do:

$('<ul>').appendTo('#object_list').append(list.contents()); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.