Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through input fields with jQuery to find the highest value?

Tags:

jquery

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.

like image 871
Tintin81 Avatar asked Oct 30 '12 23:10

Tintin81


2 Answers

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);
like image 135
VisioN Avatar answered Oct 02 '22 18:10

VisioN


Use Math.max function:

var nums = [];
$("input[type=text]").each( function() { nums.push( $(this).val() ); });
var max = Math.max.apply(Math, nums);
like image 30
SavaMinic Avatar answered Oct 02 '22 19:10

SavaMinic