How do you use jQuery filter () for an attribute that is not a class or identifier

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

Where I wrote - something here - I do not know what to do to refer to the level attribute. If it were an id attribute, I would make #idName, if it was a class, which I would make .className.

I'm not sure what to do to select a level attribute.

$ (". myClass"). filter (- something here to refer to a level attribute -). remove ();

+6
jquery filter attributes
source share
3 answers

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

+13
source share

No filter needed, just use an attribute filter , in this case it has an attribute selector :

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

This should remove all .myClass elements that have a non-empty level , of course, you could, for example, map the level based on one of several available operators (see docs ), for example startsWith :

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

contains:

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

and etc.

+11
source share

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

+1
source share

All Articles