I have a number of input[type=text]
fields on my page and I want to loop through all of them in order to find and return the highest value.
Is there a way to do this with jQuery?
Thanks for any help.
Here is one solution:
var highest = -Infinity;
$("input[type='text']").each(function() {
highest = Math.max(highest, parseFloat(this.value));
});
console.log(highest);
Here is another solution:
var highest = $("input[type='text']").map(function() {
return parseFloat(this.value);
}).get().sort().pop();
console.log(highest);
Use Math.max function:
var nums = [];
$("input[type=text]").each( function() { nums.push( $(this).val() ); });
var max = Math.max.apply(Math, nums);
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