javascript - How to add new list item to unordered list -
i have html page unordered list following (simplified):
<ul id="mylist"> <li id="item1">item 1 <a href="javascript:void(0)" class="mybutton">add</a></li> <li id="item2">item 2 <a href="javascript:void(0)" class="mybutton">add</a></li> <li id="item3">item 3 <a href="javascript:void(0)" class="mybutton">add</a></li> <li id="item4">item 4 <a href="javascript:void(0)" class="mybutton">add</a></li> <li id="item5">item 5 <a href="javascript:void(0)" class="mybutton">add</a></li> </ul>
is there way using jquery can add new list item below current one, click on corresponding button ? example: if click button item 2 new list item added between item 2 , item 3.
i wasn't sure whether use "after"
here , if, how apply in case or if there better way this.
$('#mylist').on('click', '.mybutton', function () { $(this).closest('li').after(createitem()); }); function createitem() { //return html, jquery object or dom element representing //the new item add }
please note did not bother id
number, since figured don't need id
attribute (i guess part of solution problem?).
Comments
Post a Comment