user3027531
user3027531

Reputation: 282

Append only parent UL

Following is my fiddle in which I am dynamically adding LIs. The issue I am facing is that the Parent UL in which I am trying to add Lis already consist child UL within the Parent LI and on generating dynamically a new Li. The Li not only created for parent UL but also for child UL. Kindly let me know how can I change following fiddle so the (Dynamically Created) LI will only be created for Parent <ul>

http://jsfiddle.net/NH5Lc/2/

 $("#butadd").click(function () {
        $("ul").append("<li>Folder Name : <input type='text' class='fname' value='' required='required' /> <input type='button' class='liDelete'  value='- Remove' /> <ul class='anotherUL'><li>Dynamic Child Li</li></ul> </li>");

    });

Upvotes: 0

Views: 78

Answers (2)

Musa
Musa

Reputation: 97672

$("ul").append is targetting all the uls you have to specifically select the ul you want, which is #folder

$("#folder").append("<li>Folder Name : <input type='text' class='fname' value='' required='required' /> <input type='button' class='liDelete'  value='- Remove' /> <ul class='anotherUL'><li>Dynamic Child Li</li></ul> </li>");

http://jsfiddle.net/NH5Lc/4/

Upvotes: 2

Satpal
Satpal

Reputation: 133403

As you have already provide an id to UL, use and change your selector as

$("ul#folder").append("<li>Folder Name : <input type='text' class='fname' value='' required='required' /> <input type='button' class='liDelete'  value='- Remove' /> <ul class='anotherUL'><li>Dynamic Child Li</li></ul> </li>");

DEMO

$("ul").append() will target to all uls

Upvotes: 1

Related Questions