Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selector - all but the first

I have a small jQuery selectors question, I have the following html:

<div class="member-info first"></div>
<div class="member-info"></div>
<div class="member-info"></div>

I want to hide (using jQuery) all the divs that holds the "member-info" class, but not the one holding the "first" class, any thoughts?

like image 291
Ran Avatar asked Jan 28 '11 17:01

Ran


4 Answers

$('.member-info:not(.first)').hide();

This uses the not-selector(docs) to exclude elements with the first class.

Or if the purpose of the first class is simply to identify the first, then do this instead:

$('.member-info').slice(1).hide();

This uses the slice()(docs) method to return a set starting with the second match.

like image 99
user113716 Avatar answered Nov 16 '22 14:11

user113716


Use the :not() selector. For example:

$(".member-info:not(first)").hide();

If first is really always the first child, try

$(".member-info:not(member-info:first)").hide();
like image 25
justkt Avatar answered Nov 16 '22 15:11

justkt


$(".member-info:not('.first')").hide();
$(".member-info").filter(":not('.first')").hide();
$('.member-info').not('.first').hide();

$(".member-info:not(:first)").hide();
$(".member-info").filter(":not(':first')").hide();
$('.member-info').not(':first').hide();

$(".member-info:not(:eq(0))").hide();
$(".member-info").filter(":not(':eq(0)')").hide();
$(".member-info").not(":eq(0)").hide();

$(".member-info:not(:lt(1))").hide();
$(".member-info").filter(":not(':lt(1)')").hide();
$(".member-info").not(":lt(1)").hide();

$(".member-info:gt(0)").hide();
$(".member-info").filter(':gt(0)').hide();

$(".member-info").slice(1).hide();

All possible ways that come to my mind. I also made JavaScript performance comparison where you can find some unexpectable results.

All of this samples work with v1.10.* of jQuery.

Most times this one is the fastest $(".member-info").slice(1).hide(); and looks like relevant, not brainstormed answer

like image 3
AuthorProxy Avatar answered Nov 16 '22 16:11

AuthorProxy


This doesn't quite answer your question, but you could skip the first matched element using gt.

For example:

$('div.member-info:gt(0)')

See: http://api.jquery.com/gt-selector/

like image 2
Jon Avatar answered Nov 16 '22 16:11

Jon