Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Marty Hitchcock
13,108 Pointsgetting data from a form
Hi,
I am having trouble trying to get data from a form. I set up a code pen with the concept I am talking about here: http://codepen.io/1marty4sale/pen/FvCdA
When I put something into the text field and press the button it returns blank. But when I add value="something" to the text field in the html it will return it.
Can someone please explain to me what I am doing wrong? I want to be able to get the value that was input.
2 Answers

Jason Anello
Courses Plus Student 94,596 PointsHi Marty,
The problem is that you're only getting the value of the text input one time when the page loads.
You need to get the value of the text input inside your click handler so that you're getting the value of the text input at the time the button was clicked.
Try this out:
var $textInput = $('#textInput');
var $button = $('#button');
var test = function(){
console.log($textInput.val());
};
$button.click(test);

Ismael Uriarte
10,403 Pointsvar x = "Hello, outer-space!"
console.log("Just practicing");
Marty Hitchcock
13,108 PointsMarty Hitchcock
13,108 PointsThank you! I don't think I would have worked that one out. This helps a lot!