Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete first list element using jQuery

Tags:

jquery

I have the following list:

<ul id="list">
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>6</li>    
</ul>

How can I use jQuery to remove the first element of this list (i.e. the number 1) and leave the remaining elements intact?

I'd have thought this have quite a simple solution but it's proving harder than I expected to answer.

like image 360
dplanet Avatar asked Sep 02 '12 00:09

dplanet


People also ask

How to remove first value in jQuery?

Answer: Use the JavaScript substring() method You can use the JavaScript substring() method to remove first character form a string. A typical example is removing hash ( # ) character from fragment identifier.

How to remove an element from a list in jQuery?

In jQuery, in order to remove element and content you can use anyone of the following two jQuery methods: Methods: remove() – It is used to remove the selected element (and its child elements). empty() – It is used to removes the child elements from the selected element.

How to remove appended element in jQuery?

length > 0){ var anotherToDo = $("<p>" + newToDo + "</p>"); $("#toDoList"). append(anotherToDo); $("#newToDo"). val(" "); // later when you want to delete it anotherToDo. remove(); } }); });

How to remove all div in jQuery?

To remove elements and its content, jQuery provides two methods: remove(): It removes the selected element with its child elements. empty(): It removes the child element from the selected elements.


1 Answers

$('#list li').first().remove();

jsFiddle example

Ref:

  • first
  • remove

You could also accomplish a similar effect using pure CSS3 (no JavaScript) with:

#list li:nth-child(1) {
    display:none;
}​
like image 132
j08691 Avatar answered Oct 11 '22 15:10

j08691