I have simple form set up like the following:
<form>
<input type="text" name="first_number" id="first_number" />
</form>
I want to add a button to the form that has an onclick() that gets the value of the text in the input field and sends it to my javascript function called simple_math() that has the parameter “number”. For instance:
<input type="button" onclick="simple_math(document.getElementById('first_number').value)" />
But I know that’s the incorrect syntax. Is this possible? Thank you very much!
Your code is correct, but if you are using the value in a math equation, you may need to use parseInt to convert the value from a string to an integer.
Like so: