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

Uncaught error. Cannot read property of children

Getting an error message in my console.

"Uncaught TypeError: Cannot read property 'children' of null"

my javascript code is as follows:

//problem
//user interaction doesn't provide results

//solution
//add interactivity

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

//add a new task
var addTask = function(){
    console.log("add Task...");
    //when the button is pressed
    //create a new list item with the text from #new-task
        //create an input check box
        //create label
        //create input(text)
        //create button (.edit)
        //create button (.delete)
        //each element, need to be modified and appended

}


//edit an existing task (toggle .editMode)
var editTask = function(){
    console.log("edit Task...");
    //when edit 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 = label text
}




//delete a task
var deleteTask = function(){
    console.log("delete Task...");
    //when delete butt is pressed
        //remove parent list item from the ul
}


//mark a task as complete
var taskCompleted = function(){
    console.log("task Completed...");
    //when the checkbox is checked
        //append the task list item to  #complete-tasks
}



//mark a task as incomplete
var taskIncomplete = function(){
    console.log("task Incomplete...");
    //when the checkbox is unchecked
        //append the task list item to  #incomplete-tasks

}

var bindTaskEvents = function(taskListItem, checkBoxEventHandler){
    console.log('bind list events');
    //select taskListItems 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 checkbox
        checkBox.onchange = checkBoxEventHandler;
}

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


//cycle over 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 completedTasksHolder ul list item
for(var i = 0; i < completedTasksHolder.children.length; i++){
        //bind events to list item's children(taskIncomplete)
    bindTaskEvents(completedTasksHolder.children[i], taskIncomplete);
}

1 Answer

Hey there! You forgot the d at the end of completed when you're getting the element by Id for the completedTasksHolder variable.

You have:

var completedTasksHolder = document.getElementById("complete-tasks");

Should be:

var completedTasksHolder = document.getElementById("completed-tasks");

wow, a silly little typo. Thank you so much though!

No problem! It's always the little things :)