Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Interactive Web Pages with JavaScript Selecting Elements and Adding Events with JavaScript Perform: Selecting Elements

11,593 Points

Calls to variable in console coming up as not defined

I can't see where I'm going wrong with my code to get them to populate in the console.

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

4 Answers

Steven Byington
Steven Byington
13,584 Points

Try calling the variable again after you first set it.

var taskInput = document.getElementById("new-task");

It comes up null the second time because I don't have an element with that ID, but if you do then it should show the element.

I was having this same problem and it was driving me crazy!

The following worked for me after checking that there were no errors with my code:

I saved my workspace and refreshed the preview of my To Do list. Then I re-opened the JavaScript console and it worked just fine.

By not refreshing the preview, it wasn't applying the new code from the Workspace.

Ronny Ewanek
Ronny Ewanek
4,385 Points

I also had a similar problem. Whenever something weird comes up, I've learned to painstakingly check my spelling and grammar. Here I didn't have the "By" part capitalized...

Michael Tigue
Michael Tigue
4,799 Points

I saved the app.js file again, opened the preview again, and clicked refresh on the preview tab. Then, I was able to access the variables in the console.