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
Victor Ruiz
16,570 PointsTraversing Elements with querySelector
Hi Everyone,
I'm getting an "Uncaught TypeError: Cannot read property 'children' of null" can't seem to find were I went wrong I checked where the error is happening but I can't seem to fix it. Can anyone point out what I'm missing?
//Ploblem: User interaction doesn't provide desired results.
//Solution: Add interactivity so the user can manage daily tasks.
var taskInput = document.getElementById("new-task");//new-task
var addButton = document.getElementsByTagName("button")[0];//first button
var incompleteTasksHolder = document.getElementById("incomplete-task");//incomplete-task
var completedTaskHolder = document.getElementById("completed-task");//completed-task
//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:
//input (checkbox)
//label
//input (text)
//button.edit
//button.delete
//Each elements, needs modified and appended
}
//Edit an existing task
var editTask = function() {
console.log("Edit Task...");
//When the edit button is pressed
//if the paarent 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 on the parent
}
//Delete an existing task
var deleteTask = function() {
console.log("Delete Task...");
//When the Delete button is pressed
//Remove the parent list item from the ul
}
//Mark a task as complete
var taskCompleted = function(){
console.log("Task Complete...");
// When the checkbox is checked
// Append the task list item to the #completed-tasks
}
//Mark a task as incomplete
var taskIncomplete = function(){
console.log("Task Incomplete...");
//When the checkbox is unchecked
// Append the task list item to the #incomplete-tasks
}
var bindTaskEvents = function(taskListItem, checkBoxEventHandler){
console.log("Bind list item 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 checkbox
checkBox.onchange = checkBoxEventHandler;
}
// Set the click handler to the addTask function
addButton.onclick = addTask;
//cycle over incompleteTaskHolder ul list items
for(var i = 0; i < incompleteTasksHolder.children.length; i++){
//bind events to list item's children (taskCompleted)
bindTaskEvents(incompleteTasksHolder.children[i], taskCompleted);
}
//cycle over incompleteTaskHolder ul list items
for(var i = 0; i < completedTaskHolderTasksHolder.children.length; i++){
//bind events to list item's children (taskIncomplete)
bindTaskEvents(completedTaskHolder.children[i], taskIncompleted);
}
<!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 type="checkbox"><label>Pay Bills</label><input type="text"><button class="edit">Edit</button><button class="delete">Delete</button></li>
<li class="editMode"><input 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 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 type="text/javascript" src="js/app.js"></script>
</body>
</html>
2 Answers
Julian Aramburu
11,368 PointsHi Victor! Your problem seems to be that you are targeting incomplete-task and complete-task in here:
var incompleteTasksHolder = document.getElementById("incomplete-task");//incomplete-task
var completedTaskHolder = document.getElementById("completed-task");//completed-task
but you are naming it incomplete-taskS and complete-taskS in here:
<ul id="incomplete-tasks">
<li><input type="checkbox"><label>Pay Bills</label><input type="text"><button class="edit">Edit</button><button class="delete">Delete</button></li>
<li class="editMode"><input 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 type="checkbox" checked><label>See the Doctor</label><input type="text"><button class="edit">Edit</button><button class="delete">Delete</button></li>
</ul>
So when you are trying to bind the event , it target something that doesn't exist!
Hope you find this useful!
Cheers!
Mihalis Fthenos
11,919 Pointstry putting the type in quotations.... var checkBox = taskListItem.querySelector("input[type='checkbox']");
Victor Ruiz
16,570 PointsNope the error still shows up on line 77:
//cycle over incompleteTaskHolder ul list items
for(var i = 0; i < incompleteTasksHolder.children.length; i++){
//bind events to list item's children (taskCompleted)
bindTaskEvents(incompleteTasksHolder.children[i], taskCompleted);
}
Victor Ruiz
16,570 PointsVictor Ruiz
16,570 PointsThanks I guess I missed those "s".