Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am trying to take a text value, input by the user, pass into a javascript variable and store it so it can be used in math functions after further input. Before that I need to get the inputs stored as variables. The input field id is Numb1, Once submit is pressed, I'd think the value input is passed into the firstNumb var in the assignVar function via the document.getElementbyID().

When I input a number, press submit and use the console in chrome to see what firstNumb is I get an uncaught reference error stating firstNumb is not defined.

Where am I going wrong with this?

Here is my form/input and script code

<form>
<input type="number" id="Numb1">
<input name="submit" type="submit" onclick="assignVar()">


</form>

<script>
function assignVar() {
var firstNumb = document.getElementByID("Numb1");
}

</script>

Thanks for any help

share|improve this question
up vote 3 down vote accepted

First of all, it is Id, not ID (which is why you get the error).

Second, you're getting the element itself, while were going to get its value:

var firstNumb = document.getElementById("Numb1").value;

Also, in case you wanted to convert it to a number, put + before document...:

var firstNumb = +document.getElementById("Numb1").value;

Working example:

<input type="number" id="Numb1">
<input name="submit" type="submit" onclick="assignVar()">


<script>
function assignVar() {
var firstNumb = document.getElementById("Numb1").value;
alert(firstNumb);
}

</script>

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.