Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove only the first element from a jquery selection

<div class="box"></div>
<div class="box"></div> <!-- Hide -->
<div class="box"></div> <!-- Hide -->
<div class="box"></div> <!-- Hide -->

I need to hide all this div but not the first div.

I could do something like this:

jQuery('.box').hide();
jQuery('.box').first().show();

Is there a way to remove the first .box from the array before .hide() em?

like image 817
dynamic Avatar asked Feb 02 '12 22:02

dynamic


People also ask

How do I remove the first character 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 do you select the first element with the selector statement?

The :first selector selects the first element. Note: This selector can only select one single element. Use the :first-child selector to select more than one element (one for each parent). This is mostly used together with another selector to select the first element in a group (like in the example above).

Which jQuery method is used to remove the child elements from the selected element?

The jQuery empty() method removes the child elements of the selected element(s).

Is first child jQuery?

Definition and Usage. The :first-child selector selects all elements that are the first child of their parent. Tip: Use the :last-child selector to select elements that are the last child of their parent.


1 Answers

jQuery('.box').slice(1).hide()
like image 64
zch Avatar answered Oct 09 '22 15:10

zch