How To Get Highest Input Field Value In Javascript
I want to get highest value of this field. How I can do this?
Solution 1:
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));
}
Solution 2:
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) {
returnMath.max.apply(Math, array);
};
var max = Array.max($('.input-text').map(function() {
return $(this).val();
}));
console.log(max) // 30
Solution 3:
var maxVal = 0;
$('input[name="position[]"]').each(function(){
maxVal = Math.max(maxVal , parseInt($(this).val()));
});
alert(maxVal);
Solution 4:
Try like this
HTML:
<form name="myForm">
<inputtype="text" style="width:20%;" class="input-text" name="position[]" value="20" />
<inputtype="text" style="width:20%;" class="input-text" name="position[]" value="25" />
<inputtype="text" style="width:20%;" class="input-text" name="position[]" value="10" />
<inputtype="text" style="width:20%;" class="input-text" name="position[]" value="5" />
<inputtype="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);
Solution 5:
Getting highest input value using jQuery each. Demo
var inputValue = -Infinity;
$("input:text").each(function() {
inputValue = Math.max(inputValue, parseFloat(this.value));
});
alert(inputValue);
Post a Comment for "How To Get Highest Input Field Value In Javascript"