1

How do you make the maximum value of a number input field equal to a javascript variable? This variable will change, and the max needs to change with it.

You can't define it the way you can with php, and I'm not very familiar with javascript and my searches have brought up nothing.

<input type="number" min="1" max="*jsvariable*">

Sample Fiddle: http://jsfiddle.net/KDJdZ/

cameronjonesweb
  • 2,435
  • 3
  • 25
  • 37

2 Answers2

2

First assign ID to your input:

<input id="myInput" type="number" min="1" max="*jsvariable*"/>

Then access it via javascript:

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

Or using your variable

input.setAttribute("max",your_variable); // set a new value;
Tareq Salah
  • 3,720
  • 4
  • 34
  • 48
0
<input id="inputmax" type="number" min="1" max="*jsvariable*">

//create an id for your input tag .
// in my case I used "inputmax" as id

var setmymax = document.getElementById("inputmax"); 

setmymax.setAttribute("max", "Your value");
Nizam
  • 5,698
  • 9
  • 45
  • 57