Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input type ="number" Value does not update in html is this normal

I have not used this before just now and noticed it does not update its value in html?

Basically i have a Html Table with this in it , and user will update it to a quantity needed and Submit and i will take the Html parse it to get the things.

But in the html i see the value of input box is always 1, i it never updates itself

<input type="number" id="testNumber"   value="1" min="1" max="100" />
like image 255
confusedMind Avatar asked Mar 10 '15 02:03

confusedMind


People also ask

How do I force a number input in HTML?

By default, HTML 5 input field has attribute type=”number” that is used to get input in numeric format. Now forcing input field type=”text” to accept numeric values only by using Javascript or jQuery. You can also set type=”tel” attribute in the input field that will popup numeric keyboard on mobile devices.

How do you set the value of an input type number?

The <input type="number"> defines a field for entering a number. Use the following attributes to specify restrictions: max - specifies the maximum value allowed. min - specifies the minimum value allowed.

Does input type number return number?

HTML Input element is documented to return the value in String type representing number.


2 Answers

While yes, the markup indicates that the value is still 1, if this form were to be submitted, the displayed value of the number input would still get returned.

You can verify this by running the following in your browser's console:

var input = document.getElementById('testNumber');
input.value; 

EDIT 1:

If you want the value of the html to match the value of the dom element, assign it yourself, like so:

input.setAttribute('value', input.value);
like image 69
irysius Avatar answered Oct 06 '22 08:10

irysius


This is just a sample. But it handles the INCREASE(+)/DECREASE(-) buttons clicked and updates the input value.

$("input[type=number]").click(function(e) {

    $(this).attr( 'value', $(this).val() );

});
like image 32
Giwoo Gustavo Lee Avatar answered Oct 06 '22 06:10

Giwoo Gustavo Lee