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.
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);
map() was returning array-like jQuery collection object with array prototype methods. Now we have to be strict and use get() to receive pure JavaScript array from jQuery collection. Please see the updated answer.