Basically what I want to do is when the user types a number in to an HTML form input field then presses the submit button, the number is stored in a javascript variable.
<input type="number" min="1" name="number">
var number = ?
How would I do this with javascript or with jquery?
Thank you.
Here is a javascript solution, only use this until jQuery comes built into your browser ( it might aswell be, seeing as it's the savior )
Just assign a submit handler, and get the value of the input.
My advice is to you though, please do not download jQuery just so you can store a variable.
Here's your html,
With jQuery you can do this,
With some events you can add the value of the input to the number variable,
In summary,
Hope that helps!
A simple javascript solution is to do this.
This saves the value of the text box under the name 'number'. If you know javascript you can then use it to do pretty much anything. Hope this helps.
Please note that to do this, you need to use
id="number"
, instead ofname="number"
.Complete code could be something like
this means that as soon as the user inputs a number, the save() function will be called in the javascript file.
javascript