0

Problem:

I want to insert content as the last child of an element. To reproduce what i want, i created this fiddle: http://jsfiddle.net/lexhartman/zx0r8mnu/2/

The jquery-code i am using, is the following:

jQuery(document).ready(function() {
  var $collectionHolder = $('div.collectionholder');

  var $innerDivOne = $('<li><p>TEST 1 also has to be green</p></li>');
  var $innerDivTwo = $('<li><p>TEST 2 also has to be green</p></li>');
  var $outerDivChunk = $('<div class=parentdiv><div class="subdiv"><ul class="ul-list"></ul></div></div>').append($innerDivOne).append($innerDivTwo);

  $collectionHolder.append($outerDivChunk);

});

But... the lis are being appending to the div, not the ul inside the div.

Goal:

What i want to achieve is that the two innerdiv's (li's with no classname) are inserted in the "ul" div (with classname: ul-list).

Attempts:

The outerdiv variable has to stay the same (i know that when the outerdiv only has the "ul" it works, but in my code that will bring a lot of other headaches which i won't be bothering you with.. :D).

I am guessing i will be needing an other function then append, but which one or maybe i can use append in another way then i am doing now?

Thanks!

2
  • 1
    You are not appending the lis to the list, you are appending it to the div Commented Nov 16, 2016 at 13:42
  • use the .html() function Commented Nov 16, 2016 at 13:42

1 Answer 1

2

The lis are being appending to the div, not the ul inside the div. You would need to select the ul.

var $outerDivChunk = $('<div class=parentdiv><div class="subdiv"><ul class="ul-list"></ul></div></div>');
outerDivChunk.find("ul").append($innerDivOne).append($innerDivTwo);
Sign up to request clarification or add additional context in comments.

2 Comments

You sir, are fast and effective! Thank you! Updated fiddle: jsfiddle.net/lexhartman/zx0r8mnu/6
Imagine how much faster I would be if I did not forget my coffee at home this morning. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.