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
Christopher Johnson
12,829 Pointsdocument.getElementById('#incomplete-tasks") returning null
TypeError: incompleteTaskHolder is null: app.js:75:16 (<anonymous>)
```<!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>
```//Problem: User interaction doesn't provide desired results
//Solution: Add interactivity so user can manage daily tasks.
var taskInput = document.getElementById('#new-task');
var addButton = document.getElementsByTagName('button')[0];
var incompleteTaskHolder = document.getElementById('#incomplete-tasks');
var completedTaskHolder = document.getElementById('#completed-tasks');
//Add new task(s)
var addTask = function() {
console.log('Add task...');
//when button is pressed
//create a task (new list item with the text from #new-task:
//input (checkbox)
//label
//input (text)
//button.edit
//button.delete
//Each element, needs modified and appended (modified means, for ex., an input needs to be set to checkbox)
}
//Edit existing task(s)
var editTask = function() {
console.log('Edit task...');
//when edit button is pressed
//if class of 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 on the parent
}
//Delete existing task(s)
var deleteTask = function() {
console.log('Delete task...');
//when delete button is pressed
//remove parent list item from it's parent (ul)
}
//Mark existing task(s) complete
var taskCompleted = function() {
console.log('Complete task...');
//when checkbox is checked
//append the task list item to the #completed-tasks
}
//Mark existing task(s) 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');//test if we're doing something
//select its children
var checkBox = taskListItem.querySelector(input[type="checkbox"]); //using a selector such as
var editButton = taskListItem.querySelector('button.edit'); //you can use the selector(# or .)
var deleteButton = taskListItem.querySelector('button.delete'); //you can use the selector(# or .)
//bind editTask to edit button
editButton.onlick = editTask; //refrains from calling function immediately by being referenced
//bind deleteTask to delete button
deleteButton.onlick = deleteTast;
//bind checkBoxEventHandler to checkbox
checkbox.onchange = taskCompleted;
}
//set the click handler to the addTask function
addButton.onclick = addTask;
//cycle over incompleteTasksHolder ul list items
for(var i = 0; i < incompleteTaskHolder.children.length; i++){
//bind events to list item's children (taskCompleted)
bindTaskEvents(incompleteTaskHolder.children[i], taskCompleted);
}
//cycle over completeTasksHolder ul list items
for (var i = 0; i < completedTaskHolder.children.length; i++) {
//bind events to list item's children (taskIncompleted)
bindTaskEvents(completedTaskHolder.children[i], taskIncomplete);
}
I assume I'm doing something wrong.
3 Answers
Daniel McNeil
Full Stack JavaScript Techdegree Graduate 26,471 PointsIf you're using vanilla JavaScript then you don't need the # sign:
document.getElementById('incomplete-tasks');
If you're using jQuery then you would use the # sign:
$('#incomplete-tasks');
Belve Marks
7,332 PointsHere are some hints:
There are syntax problems. The method getElementById already knows its an ID you are passing in.... The input querySelector on line 60 is incorrect. check out the MDN on that topic.
Alan Olvera
1,928 PointsYou just need to write
document.getElementById('incomplete-tasks');
the '#' is not needed when you're using the getElementById method