Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the max value for input type number on HTML5 with Javascript?

I need change the max attribute of an input element with JavaScript. For example:

<input type="number" name="miname" min="0" max="MyVar" value="0"/>
like image 494
Alberto Avatar asked Nov 28 '12 20:11

Alberto


People also ask

How do I limit the input number in HTML?

The HTML <input> tag is used to get user input in HTML. To give a limit to the input field, use the min and max attributes, which is to specify a maximum and minimum value for an input field respectively. To limit the number of characters, use the maxlength attribute.

How do I limit a number in Javascript?

If the number is less than the minimum value then give it the minimum value else if. If the number is greater than the maximum value then give it the maximum value else the number is in the range itself.

Can I change input type Javascript?

To change the type of input it is (button to text, for example), use: myButton. type = "text"; //changes the input type from 'button' to 'text'. Another way to change or create an attribute is to use a method like element.


1 Answers

You can assign an id to your input :

<input id="myInput" type="number" name="miname" min="0" max="MyVar" value="0"/>

then access it via javascript:

var input = document.getElementById("myInput");
input.setAttribute("max",100); // set a new value;

Note you can also access it directly if your input is in a form

document.formName.miname.setAttribute("max",newValue);
like image 122
Ibu Avatar answered Sep 21 '22 16:09

Ibu