[ACCEPTED]-How to append a listitem in first position of list using Jquery-jquery

Accepted answer
Score: 50


Score: 26

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.

Score: 3

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.

  $('#mylist').prepend('<li>New Item 1 added</li>')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <ul id="mylist">
  <li>First Item</li>
  <li>Second Item</li>
  <li>Third Item</li>
  <li>Fourth Item</li>  
  <button> click to add first li</button>

More Related questions