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 Selecting Elements and Adding Events with JavaScript Perform: Selecting Elements

jadaml
jadaml
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");
undefined
taskInput;
null

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.