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" />
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
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));
}
Getting highest input value using jQuery each . Demo
var inputValue = -Infinity;
$("input:text").each(function() {
inputValue = Math.max(inputValue, parseFloat(this.value));
});
alert(inputValue);
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);
var maxVal = 0;
$('input[name="position[]"]').each(function(){
maxVal = Math.max(maxVal , parseInt($(this).val()));
});
alert(maxVal);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.