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

jQuery version, can't get editTask to work properly

I recreated the to-do list with jQuery. I got everything working except the editTask function. The label will not update with the new, edited input. I can edit it but when I click 'Edit' the label remains the same as before. If I click 'Edit' again, I see in the input field the same text that I had entered previously. Any idea what I'm doing wrong?

//need an add button to trigger add task function
    //need an add task function to add task to list
//need to create list item

var $newTask = $('#new-task');
var $incompleteTaskList = $('#incomplete-tasks');
var $completeTaskList = $('#completed-tasks');
var $addButton = $('p button');

var createTask = function  (task){

    var $listItem = $('<li>');
    var $inputCheckbox = $('<input>');
    var $label = $('<label>');
    var $inputText = $('<input>');
    var $editButton = $('<button>');
    var $deleteButton = $('<button>');

    $inputCheckbox.attr("type", "checkbox").change(moveTask);
    $inputCheckbox.addClass('checkbox');
    $label.text(task);
    $inputText.attr('type', 'text');
    $inputText.addClass('text');
    $editButton.addClass('edit');
    $editButton.text('Edit')
    $deleteButton.addClass('delete');
    $deleteButton.text('Delete');


    $listItem.append($inputCheckbox).append($label).
    append($inputText).append($editButton).
    append($deleteButton);

    return $listItem;

} 

var  addTask = function () {

    var listItem = createTask($newTask.val());

    $incompleteTaskList.append(listItem);
    $newTask.val(""); 


}

var deleteTask = function(){
    $(this).parent().remove();
}

var editTask = function(){

    var $editableUserInput = $(this).siblings('.text');
    var $label = $(this).siblings('label');

    if ($(this).parent().hasClass('editMode')){
        $label.text($editableUserInput.val());


    } else {
        $editableUserInput.val($label.text());

    }
    $(this).parent().toggleClass('editMode');
}

var moveTask = function(){
    if($(this).prop('checked')===true){
        $completeTaskList.append($(this).parent());
    } else{
        $incompleteTaskList.append($(this).parent());
    }
}

$.each($('.checkbox'), function(){
    $(this).change(moveTask);
});



$('.edit').click(editTask);


$('.delete').click(deleteTask);

$addButton.click(addTask);

HTML

<!DOCTYPE html>
<html>
  <head>
    <title>Todo App</title>
    <link href='http://fonts.googleapis.com/css?family=Lato:300,400,700' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" charset="utf-8">    
  </head>
  <body>
    <div class="container">

      <p>
        <label for="new-task">Add Item</label><input id="new-task" type="text"><button >Add</button>
      </p>


      <h3>Todo</h3>
      <ul id="incomplete-tasks">
        <li><input class="checkbox" type="checkbox"><label>Pay Bills</label><input type="text"><button class="edit">Edit</button><button class="delete">Delete</button></li>
        <li class="editMode"><input class="checkbox" type="checkbox"><label>Go Shopping</label><input type="text" value="Go Shopping"><button class="edit">Edit</button><button class="delete">Delete</button></li>

      </ul>

      <h3>Completed</h3>
      <ul id="completed-tasks">
        <li><input class="checkbox" type="checkbox" checked><label>See the Doctor</label><input type="text"><button class="edit">Edit</button><button class="delete">Delete</button></li>
      </ul>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
    <script type="text/javascript" src="js/app.js"></script>

  </body>
</html>

CSS

/* Basic Style */
body {
  background: #fff;
  color: #333;
  font-family: Lato, sans-serif;
}
.container {
  display: block;
  width: 400px;
  margin: 100px auto 0;
}
ul {
  margin: 0;
  padding: 0;
}
li * {
  float: left;
}
li, h3 {
  clear:both;
  list-style:none;
}
input, button {
  outline: none;
}
button {
  background: none;
  border: 0px;
  color: #888;
  font-size: 15px;
  width: 60px;
  margin: 10px 0 0;
  font-family: Lato, sans-serif;
  cursor: pointer;
}
button:hover {
  color: #333;
}
/* Heading */
h3,
label[for='new-task'] {
  color: #333;
  font-weight: 700;
  font-size: 15px;
  border-bottom: 2px solid #333;
  padding: 30px 0 10px;
  margin: 0;
  text-transform: uppercase;
}
input[type="text"] {
  margin: 0;
  font-size: 18px;
  line-height: 18px;
  height: 18px;
  padding: 10px;
  border: 1px solid #ddd;
  background: #fff;
  border-radius: 6px;
  font-family: Lato, sans-serif;
  color: #888;
}
input[type="text"]:focus {
  color: #333;
}

/* New Task */
label[for='new-task'] {
  display: block;
  margin: 0 0 20px;
}
input#new-task {
  float: left;
  width: 318px;
}
p > button:hover {
  color: #0FC57C;
}

/* Task list */
li {
  overflow: hidden;
  padding: 20px 0;
  border-bottom: 1px solid #eee;
}
li > input[type="checkbox"] {
  margin: 0 10px;
  position: relative;
  top: 15px;
}
li > label {
  font-size: 18px;
  line-height: 40px;
  width: 237px;
  padding: 0 0 0 11px;
}
li >  input[type="text"] {
  width: 226px;
}
li > .delete:hover {
  color: #CF2323;
}

/* Completed */
#completed-tasks label {
  text-decoration: line-through;
  color: #888;
}

/* Edit Task */
ul li input[type=text] {
  display:none;
}

ul li.editMode input[type=text] {
  display:block;
}

ul li.editMode label {
  display:none;
}
Steven Parker
Steven Parker
243,656 Points

Is there some code missing here, maybe some HTML and/or CSS?

I've added the HTML and CSS