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.

JavaScript

Marty Hitchcock
Marty Hitchcock
13,108 Points

getting 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

Hi 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);
Marty Hitchcock
Marty Hitchcock
13,108 Points

Thank you! I don't think I would have worked that one out. This helps a lot!

Ismael Uriarte
Ismael Uriarte
10,403 Points
var x = "Hello, outer-space!"

console.log("Just practicing");