ilivewithian ilivewithian - 4 months ago 21
jQuery Question

jQuery insertAt

I'm trying to insert an li element into a specific index on a ul element using jQuery. I only seem to be able to insert an element on the end of the list. I am very new to jQuery, so I may just not be thinking properly.

Answer

Try something like this:

$("#thelist li").eq(3).after("<li>A new item</li>");

With the eq function, you can get a specific index of the elements retrieved...then, insert the new list item after it.

In the above function, I am inserting a new item at position 4 (index 3).

More info about the function at the jQuery Docs