views:

149

answers:

3

I want to filter based on an attribute called "level".

Where I have written -- something here -- I don't know what to do to reference the level attribute. If it was an id attribute I would do #idName if it was a class I would do .className.

I am not sure what to do to select the level attribute.

$(".myClass").filter(--something here to reference the level attribute --).remove();

+7  A: 

filter("[level='2']")

Coronatus
+5  A: 

No need for filter, just use the attribute filter syntax, in this case, the Has Attribute Selector:

$(".myClass[level]").remove();

That should remove all .myClass elements that have a non-empty level, of course, you could for instance match level's based on one of the several available operators (see the docs), such as startsWith:

$(".myClass[level^=foo]").remove(); // remove the ones that start with 'foo'

contains:

$(".myClass[level*=haa]").remove(); // remove the ones that contain 'haa'

etc.

karim79
+1  A: 

If you need to do something complicated, you can write a filter function that returns true for the elements that should be in the filtered set.

Rob Tanzola
Whilst this is not the best approach in this case, for completeness sake, this could be done as follows: `.filter(function () { return this.level == 'something'; });` (where `this` is the current DOM element)
Matt
Agreed - I gave +1's to the nice solutions below. Thanks for the comment.
Rob Tanzola