I have 2 divs nested with a ul inside them.
<div id="slide1">
<div class="images">
<ul>
</ul>
</div>
</div>
I wanted to add a li dynamically to the ul using jquery. The selector I am using is..
$("#slide1 > div.images ul").append(
It does not work. Where am I wrong?
Thanks a lot in advance,
I have 2 divs nested with a ul inside them.
<div id="slide1">
<div class="images">
<ul>
</ul>
</div>
</div>
I wanted to add a li dynamically to the ul using jquery. The selector I am using is..
$("#slide1 > div.images ul").append(
It does not work. Where am I wrong?
Thanks a lot in advance,
Share Improve this question asked Dec 24, 2013 at 21:51 pessipessi 6791 gold badge11 silver badges26 bronze badges 3- 4 That's the correct selector. Maybe the problem is with the LI you're trying to add. Show the rest of the code. – Barmar Commented Dec 24, 2013 at 21:54
- the selector is right, what is the code after append? – Daiwei Commented Dec 24, 2013 at 22:01
- silly mistake on my part..with variable name..works now. – pessi Commented Dec 24, 2013 at 22:09
2 Answers
Reset to default 4You code should work you just need to define the li
var li = $('<li></li>');
li.html('Test!');
$("#slide1 > div.images ul").append(li);
Or as PSL pointed out, just: var li = $('<li>', {'html':'Test!'});
Demo
if you want to append a li element to the end of your ul:
$("#slide1 > div.images ul").append($("<li>my new li</li>"));
or if you want to add a li to the beginning of your ul
$("#slide1 > div.images ul").prepend($("<li>my new li</li>"));