Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery remove first item from ordered list

I have an unordered list that I am prepending data to as follows:

jQuery("#mylist").prepend(newItem);  

When the list reaches a certain size I need to remove the first item that was inserted before adding the new one.

How would I get the first item to remove based on accessing the ordered list by it's id.

Something like:

 jQuery("#mylist")[0].remove();

Thanks

like image 329
JIbber4568 Avatar asked Nov 13 '12 17:11

JIbber4568


1 Answers

Since you mentioned ordered list, im assuming #mylist contains li tags inside, thus this should work

jQuery("#mylist li:first-child").remove();

I see you are prepending there, In case you want to remove the last element then

jQuery("#mylist li:last-child").remove();
like image 180
Atif Avatar answered Sep 28 '22 07:09

Atif