Ankur
Ankur

Reputation: 51100

jQuery: Selecting all elements where attribute is greater than a value

I know that to filter an element with an atttribute called attrName which has value attrValue I do:

filter("[attrName='attrValue']")

but looking at the docs http://api.jquery.com/category/selectors/ I can't see an option to select all elements s.t. attrName>attrValue

Will this work

   filter("[attrName>'attrValue']")

Upvotes: 77

Views: 46959

Answers (4)

Fangxing
Fangxing

Reputation: 6115

For ES6 arrow function

$("selector").filter((index, el) => {
    return parseInt($(el).attr("attrName")) > 123;
});

Upvotes: 0

Julien Le Coupanec
Julien Le Coupanec

Reputation: 7988

Be careful, if you are playing with integers make sure you are using parseInt().

$("selector").filter(function() {
    return parseInt($(this).attr("my-attr")) > 123;
});

Upvotes: 30

Crozin
Crozin

Reputation: 44376

The solution is jQuery.filter():

$("selector").filter(function() {
    return  $(this).attr("my-attr") > 123;
});

Upvotes: 29

Nick Craver
Nick Craver

Reputation: 630389

You can do this using the function overload of .filter(), like this:

.filter(function() {
  return $(this).attr("attrName") > "someValue";
})

Upvotes: 113

Related Questions