3

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" />

5 Answers 5

2

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));
}
Sign up to request clarification or add additional context in comments.

Comments

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) {
    return Math.max.apply(Math, array);
};

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

console.log(max) // 30

JSFiddle Link

Comments

1
var maxVal = 0;
$('input[name="position[]"]').each(function(){
    maxVal = Math.max(maxVal , parseInt($(this).val()));
});
alert(maxVal);

Comments

1

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);

Comments

1

Getting highest input value using jQuery each. Demo

var inputValue = -Infinity;
$("input:text").each(function() {
    inputValue = Math.max(inputValue, parseFloat(this.value));
});
alert(inputValue);

2 Comments

i cannot put on input there are also other other input field too
You can use text selector

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.