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/

2

2 Answers 2

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

4 Comments

I can get it to work off the bat, but I need it to change as the variable changes. See fiddle: jsfiddle.net/KDJdZ
I look at your code at js fiddle I think the problem in the time you load JavaScript .. in the left corner You will see that JavaScript work on load. Change it to No warp <in body>
How do I reflect that in a webpage?
Add you script tag after the body is loaded look at this jsbin.com/IrUBebA/2/edit
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");

Comments

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.