Reputation: 620
Is there a nice way to accomplish filtering selected elements down to a few classes? I know I could do them one at a time, but this just seemed to be something jQuery would allow.
These are being served up with ajax, and I don't have access to define the actual html.
$('.val>td').each(function () {
$(this).filter('.price,.quantity,.remove').children().children().addClass('hidetaxfields');
});
Upvotes: 15
Views: 27982
Reputation: 15472
Using the .is()
method should work:
$('.val>td').each(function () {
var $this = $(this);
if( $this.is('.price, .quantity, .remove') ){
$this.children().children().addClass('hidetaxfields');
}
});
But this is even better:
$('.val>td.price, .val>td.quantity, .val>td.remove').each(function () {
$(this).children().children().addClass('hidetaxfields');
});
or this:
var $tds = $('.val>td').filter('.price, .quantity, .remove');
$tds.each(function () {
$(this).children().children().addClass('hidetaxfields');
});
Upvotes: 2
Reputation: 30416
With filter you can write a filter function that can do this like so (demo):
$('.val>td').filter(function () {
var that = $(this);
return that.hasClass('price') || that.hasClass('remove') || that.hasClass('quantity');
}).addClass('hidetaxfields');
Upvotes: 3
Reputation: 60413
What you are asking isnt clear from the exmple you give...
This will produce a subset of the elements matched by the inital selector that have the class one
OR two
:
$(selector).filter('.one, .two');
This will produce a subset of the elements matched by the inital selector that have BOTH the classes one
AND two
:
$(selector).filter('.one.two');
Upvotes: 24