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

Hayden Bradfield
Hayden Bradfield
1,797 Points

Why aren't my querySelectors working?

There seems to be an error in the console when I try to select the checkBox, editButton, or deleteButton

An error message appears: Uncaught Error: Cannot read property querySelector of undefined(_)

//Problem: User interaction doesnt provide desired results
//Solution: Add interactivity so the user can manage daily tasks

var taskInput = document.getElementById("new-task");
var addButton = document.getElementsByTagName("button")[0]; //First Button
var incompleteTasksHolder = document.getElementById("incomplete-tasks"); 
var completedTasksHolder = document.getElementById("completed-tasks"); 

var addTask = function() {
//Add a new task
  console.log("Add Task...");
  //When the button is pressed
    //Create a new list item from #new-task
        //input checkbox
        //label
        //input (text)
        //button .edit
        //button .delete
        // Each elements needs modified and appended
}

var editTask = function() {
//Edit an existing task
  console.log("Edit Task...");
    //When the edit button is pressed
        //if li is .editmode
            //Switch from .editmode
            //label text become input value
        //else
            //Switch to .editmode
            //input value becomes label's text

        //Toggle .editmode on the parent 
}


var deleteTask = function() {
//Delete an existing task
  console.log("Delete Task");
    //When delete button is pressed
      //remove parent list item from the unordered list
}

var taskCompleted = function() {
//Mark a task as complete
   console.log("Complete Task...");
    //When the checkbox is checked
      //Append the task list item to the #completed-tasks
}

var taskIncompleted = function() {
//Mark a task as incomplete
  console.log("IncompleteTask...");
  //When checkbox is unchecked
    //Append the list item to #incomplete-tasks
}

// WIRING 

var bindTaskEvents = function(taskListItem, checkBoxEventHandler) {
    console.log("Bind list items...");
    //select taskListItem's children
    var checkBox = taskListItem.querySelector("input[type=checkbox]");
    var editButton = taskListItem.querySelector("button.edit");
    var deleteButton = taskListItem.querySelector("button.delete");
        //bind editTask to the edit button
        editButton.onclick = editTask;
        //bind deleteTask to delete button
        deleteButton.onclick = deleteTask;
        //bind checkBoxEventHandler to checkbox

}

//Set the click handler to the addTask function
addButton.onclick = addTask;

//cycle over incompleteTasksHolder ul list items
for (var i = 0; i < incompleteTasksHolder.children.length; i++) {
    //for each list item
        //bind events to list item's children (taskCompleted)
        bindTaskEvents(incompleteTasksHolder.children[1], taskCompleted);
}   

//cycle over completedTasksHolder ul list items
    for (var i = 0; i < completedTasksHolder.children.length; i++) {
    //for each list item
        //bind events to list item's children (taskIncompleted)
        bindTaskEvents(completedTasksHolder.children[1], taskIncompleted);
}   

1 Answer

Bryan Peters
Bryan Peters
11,996 Points

Shouldn't these lines use [i] instead of [1] otherwise you are only binding to second item? If your list is empty then the second item would be undefined.

bindTaskEvents(incompleteTasksHolder.children[1], taskCompleted);
 bindTaskEvents(completedTasksHolder.children[1], taskIncompleted);