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!
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

Adam maitland
Courses Plus Student 211 Points.value not updating ?
Here is my code :
<html>
<body>
<div class="wrapper">
<input type="text" id="name">
<input type="submit" value="send" id="button">
</div>
<script>
var button = document.getElementById('button');
function get() {
var user_name = document.getElementById("name").value;
console.log(user_name);
}
button.onclick = get();
</script>
</body>
</html>
The console.log
should return the value of the input into the console but nothing happens! I have tried many different ways but nothing seems to be working.
5 Answers

Chris Shaw
26,676 PointsHi Adam,
First up, please have a read of Posting Code to the Forum when posting code, I have updated your post to fix the formatting.
You have a simple issue with your code which is you're executing your get
function rather than assigning it as a reference which will prevent your onclick
event from working along with printing an empty value to your console when the page loads. See the below for the correct code.
button.onclick = get;
Happy coding!

Dylan Aresu
9,687 Pointstry .val instead of .value :)

Adam maitland
Courses Plus Student 211 PointsThanks for your answer but that just returns undefined

Seth Kroger
56,412 Points.val is for jQuery, not vanilla JavaScript.

Dylan Aresu
9,687 Pointsinstead of "name" try name and ...
document.getElementById(name).value ??

Adam maitland
Courses Plus Student 211 PointsThank you !!!! its working now thanks again

Dylan Aresu
9,687 Pointsif all else fails use JQUERY.. this will 100% work. just link the jquery library at the bottom of your html page in a script src like this.
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script> <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
//this does the exact same thing. $("#user_name").val()

Chris Shaw
26,676 PointsjQuery isn't a solution as it adds an enormous amount of overhead to a page for something that can be achieved with one line of JavaScript.
Adam maitland
Courses Plus Student 211 PointsAdam maitland
Courses Plus Student 211 PointsThanks for your answer Chris but is still coming back as undefined ? here is the code with your update:
and the javascript
Chris Shaw
26,676 PointsChris Shaw
26,676 PointsYour original
get
function had nothing wrong with it,.val
should be.value
.