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

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");