0

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

1 Answer 1

3

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>

Sign up to request clarification or add additional context in comments.

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.