Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
ayezee33
16,096 PointsPass html input to JS variable and log to console.
I obviously need to rewatch this track as this was somewhat covered and pretty basic stuff. Nevertheless, I would like to pass a form input to a variable and then log it to the console. The ultimate goal of this is to make a simple calculator.
Here is my basic HTML
<form id="calculator">
<input id="users" type="number" />
<button type="submit">Submit</button
And the Javascript
function usersInput() {
var users = document.getElementById("users").value;
}
usersInput();
console.log(users);
1 Answer
ayezee33
16,096 PointsI tried your suggestion Matthew and although it didn't return an error it didn't log to the console.
I tried calling the function with
usersInput();
But no dice...any other suggestions?
Matthew Polland
9,652 PointsHmm, well I can't see your full situation but it seems that button doesn't have a value, you are probably trying to pull a value off of a text field which should have a different ID.
Matthew Polland
9,652 PointsMatthew Polland
9,652 PointsYour users variable is in the scope of the usersInput function and is not accessible outside of the function. Try returning the value in the function, then you can do var users = usersInput(), like so: