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

Quick help with jQuery and JS.

Hi! Just writing out some Javascript for a site I am working on and I am using jQuery to help out.

However, I have encountered an error. I am trying to print the value of the variable I have created but it is returning 'undefined' in the alert box! I haven't done JS in a while so any pointers and help are greatly appreciated!

$("#registerButton").click(function() {
    var hotelName = $("#hotelName").val();
    alert(hotelName);
});

Thank you!

-Luke

1 Answer

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,254 Points

There's not a lot of code to go on here but it looks like you're trying to get the value of an empty variable. I know you've assigned #hotelName to it but I'm assuming that's the id to a div with that name. You're trying to get a value to a variable but one that you can't do anything with.

My thought is that if you want whatever is contained in #hotelName you might want to use innerHTML rather than val?

But I may have got the wrong end of the stick with what you want. :-)

Hi Jonathan!

Thank you for the reply! Sorry, I have just realised how little code I have included! The #hotelName element is an input field for text in this case.

Thank you for all your help thus far!