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


Nancy Melucci
Nancy Melucci
Courses Plus Student 35,075 Points

Make variables out of form fields

I am trying to make two form fields, one that includes a date picker, into variables so I can pass them to a dynamic list. What is preventing them from being saved to variable values? There is some debugging code included (myvar, just trying to see if I could make any variable at all.)


<div class="content">
<h2>To-Do List</h2>
<ul id="listView">
<form id="todo">

<input type="text" name="task"  id="task" placeholder="Add a task"/></br>
<input type="text" name="date" id = "datePicker" placeholder="Choose a date"/></br>
<input type="submit" id="mybutton" value="Add" />
var myvar = 10;

$('mybutton').click(function() { 
    var task = $('task').val();     
$('[type="text"]').change(function() {
    var date = $(this).datepicker("getDate");

2 Answers

Steven Parker
Steven Parker
227,141 Points

In an ID selector, the name must be prefixed with a "#" symbol:

  var task = $("#task").val();

Also, did you really mean to install the "change" event handler inside the "click" handler? As is, it will only work afte the button has been pushed.

Nancy Melucci
Nancy Melucci
Courses Plus Student 35,075 Points

Thank you. I will tinker with it later and make these changes.