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

Nancy Melucci
seal-mask
.a{fill-rule:evenodd;}techdegree
Nancy Melucci
Front End Web Development Techdegree Student 34,491 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
216,083 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.