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 Interactive Web Pages with JavaScript Traversing and Manipulating the DOM with JavaScript Perform: Traversing Elements with querySelector

Jennifer Hughes
Jennifer Hughes
11,421 Points

Uncaught TypeError: Cannot read property 'querySelector' of undefined

This error seems to be a common one, but I cannot figure out what I've done wrong. It's been that kind of day! Thanks in advance for your help.

//Problem: User interaction doesn't provide desired results
//Solution: Add interactivty so the user can manage daily tasks

//Add a new task
var taskInput = document.getElementById("new-task"); //#new-task
var addButton = document.getElementsByTagName("button")[0]; //first button
var incompleteTasksHolder =document.getElementById("incomplete-tasks"); //unordered list #incomplete-tasks
var completedTasksHolder =document.getElementById("completed-tasks"); // #completed-tasks

var addTask = function () {
  console.log("add task")


  //When the button is pressed
  //Create a new list item with the text from #new-task:
    //input (checkbox)
    //label
    //input (text) when we edit the task
    //button .edit
    //button .delete
    //Each element will need to be modified and appended
}

var editTask = function() {
    console.log("edit task")

//Edit an existing task
  //When the edit button is pressed
    //if the class of the parent is .editMode
      //switch from .editMode
      //label text become the input's value
    //else
      //switch to editMode 
      //input value becomes the label's text

      //Toggle .editMode
}

var deleteTask = function() {
    console.log("delete task")

//Delete an existing task
  //When the delete button is pressed
    //Remove the parent list item from the unordered list
}

var taskCompleted = function() {
    console.log("complete task")

//Mark a task as complete
  //When the checkbox is checked
    //Append the task list item to the #completed-tasks
}

var taskIncomplete = function() {
    console.log("incomplete task")

//Mark a task as incomplete
  //When the checkbox is unchecked
    //append the task list item to the #incomplete-tasks
}

//WIRING

var bindTaskEvents = function(taskListItem, checkBoxEventHandler) {
  console.log("Bind listitem events");
  //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 edit button
  editButton.onclick = editTask;

  //bind deleteTask to delete button 
  deleteButton.onclick = deleteTask;

  //bind checkBoxEventHandler to the checkbox
  checkbox.onchange = checkBoxEventHandler;

}

//Set the click handler to the addTask function

addButton.onclick = addTask;

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

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

2 Answers

Hey again Jennifer,

In the very bottom for loop, in the for loop itself, it should be iterating over the completedTasksHolder not incompleteTasksHolder.

//changed incompleteTasksHolder to completedTasksHolder
for(var i = 0; i < completedTasksHolder.children.length; i++) {
  //bind events to list item's children(taskCompleted)
  bindTaskEvents(completedTasksHolder.children[i], taskIncomplete);
  } 

You're welcome :)