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" />
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));
}
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
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