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?
$('.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.
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();
$(".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
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With