Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get highest input field value in javascript

I want to get highest value of this field. How I can do this?

 <input type="text" style="width:20%;" class="input-text" name="position[]" value="20" />
 <input type="text" style="width:20%;" class="input-text" name="position[]" value="25" />
 <input type="text" style="width:20%;" class="input-text" name="position[]" value="10" />
 <input type="text" style="width:20%;" class="input-text" name="position[]" value="5" />
 <input type="text" style="width:20%;" class="input-text" name="position[]" value="30" />
like image 686
Qaisar Satti Avatar asked Nov 27 '25 01:11

Qaisar Satti


2 Answers

Pure javascript:

var inputs = document.querySelectorAll('input[name="position[]"]');
var max =0;
for (var i = 0; i < inputs.length; ++i) {
   max =  Math.max(max , parseInt(inputs[i].value));
}
like image 115
Samir Das Avatar answered Nov 29 '25 16:11

Samir Das


Others may chime in with a vanilla solution, but if you are using jQuery here is a way you can do so

Array.max = function(array) {
    return Math.max.apply(Math, array);
};

var max = Array.max($('.input-text').map(function() {
    return $(this).val();
}));

console.log(max) // 30

JSFiddle Link

like image 41
scniro Avatar answered Nov 29 '25 17:11

scniro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!