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


David Jarrin
David Jarrin
Courses Plus Student 11,182 Points

JavaScript variables within a php file wont show up in console.

This has really been bothering me. when I define a variable like so:

var submit_listener = document.getElementById('survey_form');

Then I try to type submit_listener into the console I get this error message:

Uncaught ReferenceError: submit_listener is not defined
    at <anonymous>:2:1
    at Object.InjectedScript._evaluateOn (<anonymous>:895:140)
    at Object.InjectedScript._evaluateAndWrap (<anonymous>:828:34)
    at Object.InjectedScript.evaluate (<anonymous>:694:21)

but I have definitly had cases where I type in the variable and it tells me what its value is.

My only theory is that the current file I am working with is within a php file? Please help, I find this very annoying and I want to know what I am doing wrong here.

1 Answer

Hi David,

Is it because the variable is inside a function? If its in the global scope you should be able to see it in the console but if its buried within a function then not. This includes even a $(document).ready(function() {}

You could always add a console.log() to expose the variable as well.

Hope that helps.

David Jarrin
David Jarrin
Courses Plus Student 11,182 Points

That's a good point, I had the variables between script tags that a co-worker was using and he had some functions in there....they all appeared to be closed (semi-colons and all) out and then my code begins....? Idk the problem is solved if I put my code in a separate script tags. Thanks for the answer!

No worries David, glad you got it sorted :-D