dynamic
dynamic

Reputation: 48091

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?

Upvotes: 15

Views: 18921

Answers (5)

Rafay
Rafay

Reputation: 31033

try

jQuery('.box').not(':first').hide();

comparison:

@T.J.Crowder is right the code i have suggested does the extra parsing that can be avoided by .slice as suggested by @zch

HERE is the profile of my code (0.8ms) and HERE is the profile of @zch's code (0.53ms) see the difference

Upvotes: 6

Envious
Envious

Reputation: 604

jQuery('.box:not(:first)').hide();

http://jsfiddle.net/8wMFc/

Upvotes: 13

zch
zch

Reputation: 15278

jQuery('.box').slice(1).hide()

Upvotes: 38

Royi Namir
Royi Namir

Reputation: 148524

try :

jQuery('.box:gt(0)').hide();

Upvotes: 1

Patrickdev
Patrickdev

Reputation: 2381

jQuery('.box').not(':eq(0)').hide();

That said, I prefer Residual Envy's solution.

Upvotes: 1

Related Questions