Reputation: 22241
What is the jQuery alternative to not(':not( selector )')?
Basically lets say this:
var buttons = $('a.buttons');
I am looking for a particular button with the href as '#measurement' and need to add a class to it. The only way I know how to do this is with not(':not( selector )').
buttons.not(':not([href="#measurement"])').addClass('selected');
There has got to be a better way.
.is() // returns boolean
.has() // looks for items inside each element
Any thing out there?
Upvotes: 2
Views: 941
Reputation: 227270
The 2 not
s cancel out, and you get
$('a.buttons[href="#measurement"]').addClass('selected');
Docs: http://api.jquery.com/category/selectors/attribute-selectors/
EDIT: If you already have a collection, use .filter
var buttons = $('a.buttons');
buttons.filter('[href="#measurement"]').addClass('selected');
Upvotes: 4
Reputation: 198388
I believe you want filter
:
$elements.filter(selector)
so if you already have
var $buttons = $('a.buttons');
you can get the right one by
var $theButtonIWant = $buttons.filter('[href*="#measurement"]');
Upvotes: 4
Reputation: 3023
var button = $('a.buttons[href*="#measurement"]').addClass('selected');
The [ ] block lets you specify an attribute. The *= operator in it specifies that the attribute contains the quoted text.
Upvotes: 1