Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();

like image 398
Ankur Avatar asked Apr 05 '10 13:04

Ankur


People also ask

How do we filter out elements using jQuery?

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.

What is the jQuery filtering method that returns elements that do not match a condition?

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.

Which type of parameter can jQuery filter function take?

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.

What is the following syntax is used to apply not equal filter on HTML elements using jQuery?

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”.


2 Answers

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

like image 74
Amy B Avatar answered Sep 23 '22 08:09

Amy B


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.

like image 34
karim79 Avatar answered Sep 22 '22 08:09

karim79