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" />
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" />
Share
Improve this question
edited Jun 9, 2015 at 4:49
scniro
17k8 gold badges66 silver badges107 bronze badges
asked Jun 9, 2015 at 4:29
Qaisar SattiQaisar Satti
2,7622 gold badges20 silver badges36 bronze badges
5 Answers
Reset to default 2Pure 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
var maxVal = 0;
$('input[name="position[]"]').each(function(){
maxVal = Math.max(maxVal , parseInt($(this).val()));
});
alert(maxVal);
Try like this
HTML:
<form name="myForm">
<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" />
</form>
Javascript:
var myForm = document.forms.myForm;
var myControls = myForm.elements['position[]'];
var max = -Infinity;
for (var i = 0; i < myControls.length; i++) {
if( max<parseInt(myControls[i]))
max=parseInt(myControls[i]);
}
console.log(max);
Getting highest input value using jQuery each. Demo
var inputValue = -Infinity;
$("input:text").each(function() {
inputValue = Math.max(inputValue, parseFloat(this.value));
});
alert(inputValue);