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

JqueryCss SelectorsInequalities

Jquery Problem Overview


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']")

Jquery Solutions


Solution 1 - Jquery

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

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

Solution 2 - Jquery

The solution is jQuery.filter():

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

Solution 3 - Jquery

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

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

Solution 4 - Jquery

For ES6 arrow function

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnkurView Question on Stackoverflow
Solution 1 - JqueryNick CraverView Answer on Stackoverflow
Solution 2 - JqueryCrozinView Answer on Stackoverflow
Solution 3 - JqueryJulien Le CoupanecView Answer on Stackoverflow
Solution 4 - JqueryfangxingView Answer on Stackoverflow