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();
The filter() method returns elements that match a certain criteria. This method lets you specify a criteria. Elements that do not match the criteria are removed from the selection, and those that match will be returned. This method is often used to narrow down the search for an element in a group of selected elements.
jQuery | filter() with Examples The filter() method is used to filter out all the elements that do not match the selected criteria and those matches will be returned. Parameters: criteria : It specifies a selector expression, a jQuery object or one or more elements to be returned from a group of selected elements.
The jQuery filter() method can take the selector or a function as its argument to filters the set of matched elements based on a specific criteria.
The not() is an inbuilt function in jQuery which is just opposite to the filter() method. This function will return all the element which is not matched with the selected element with the particular “id” or “class”.
filter("[level='2']")
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.
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