JQuery selects elements with a value between x and y

           

I would like to make this choice:

$('input[value between 6 and 11]');

Which would give me 9 and 10. Is there a way to do this?

+5
source share
3 answers

You can do this using the .filter()following:

$("input").filter(function() { 
  return $(this).val() >= 6 && $(this).val() <= 11; 
}).somethingHere();

If you need to use this often, you can also make it a selector:

jQuery.expr[':'].between = function(a, b, c) { 
   var args = c[3].split(',');
   var val = parseInt(jQuery(a).val());
   return val >= parseInt(args[0]) && val <= parseInt(args[1]);
};

Then you can choose:

$("input:between(6, 11)")

Just change >=and <=if you do not want the range to be included.

+15
source

You can use function filter :

$('input').filter(function(index) {
    var value = parseInt($(this).val());
    return !isNaN(value) && value > 6 && value < 11;
});
+4
source

You can use .filter():

$("input").filter(function(){
   var v = parseInt($(this).val());
   return (v > 9 && v < 11)
});
+2
source

All Articles