2

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,

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. Commented Dec 24, 2013 at 21:54
  • the selector is right, what is the code after append? Commented Dec 24, 2013 at 22:01
  • silly mistake on my part..with variable name..works now. Commented Dec 24, 2013 at 22:09

2 Answers 2

4

You 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

Sign up to request clarification or add additional context in comments.

Comments

2

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>"));

Comments

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.