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

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 35,157 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.)

<body>

<div class="content">
<h2>To-Do List</h2>
</br>
<ul id="listView">
</ul>
<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" />
</br>
</form>
</div>
 </body>
</html>
var myvar = 10;


$('mybutton').click(function() { 
    //event.preventDefault();       
    var task = $('task').val();     
    $('[type="text"]').datepicker();
$('[type="text"]').change(function() {
    var date = $(this).datepicker("getDate");
    alert("splunge");
    console.log(myvar);
    console.log(date);
    console.log(task);
});
});  

2 Answers

Steven Parker
Steven Parker
229,644 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
PLUS
Nancy Melucci
Courses Plus Student 35,157 Points

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