[ACCEPTED]-How to append a listitem in first position of list using Jquery-jquery
Accepted answer
$('#mylist').prepend('<li></li>')
0
You are selecting the second li
element, try 1 this:
$('#mylist li:eq(0)').before("<li>first</li>");
//or $('#mylist li:first')...
or you can use prepend
method.
This snippet will solve the problem. Or 1 go to the link to learn more.
$('#mylist').prepend('<li>New Item</li>');
//use jQuery's prepend method.
$('button').on('click',function(){
$('#mylist').prepend('<li>New Item 1 added</li>')
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<html>
<body>
<ul id="mylist">
<li>First Item</li>
<li>Second Item</li>
<li>Third Item</li>
<li>Fourth Item</li>
</ul>
<button> click to add first li</button>
</body>
</html>
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.