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 trialMarty 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,610 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!