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 jQuery Basics (2014) Creating a Mobile Drop Down Menu Setting a value

Can't get a label to display a prompt's input value with jQuery. Also don't understand "prompt(...)" provided here

This quiz has stumped me for days. This is the best solution I've come up with to get the label to display the value that is captured by the prompt input. Is there more I'm missing from the var fullName to store it? Also, I didn't see prompt() in the jQuery API so I don't fully understand what the function does. Please help! Any hints?

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<label for="fullName">Full Name</label><input id="fullName" name="fullName" value="" disabled>
<script src="//code.jquery.com/jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/app.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>
jQuery
//Show Prompt Window and store value
var fullName = prompt("What is your full name?");

//Select Input with the id of #fullName

//Insert value in to full name input

$("#fullName").val($fullName.text());

2 Answers

Seth Kroger
Seth Kroger
56,413 Points

val() can take a string or number value, so there's no need to use text() since fullName should already be a string. There's also no dollar sign on fullName. $fullName would be a different variable altogether.

$('#fullName').val(fullName);

Wow that worked. Thank you, Seth!

In the jQuery Basics class I'm taking we are using $ in front of variables all the time (see example code from the class below). I thought Andrew said in a previous video that it isn't necessary, but a convention.

But if I try .val($fullName) in my Code Challenge, it throws a reference error saying it can't find the variable. So why is it okay in workspaces with the code below?

//cycle over menu links 
$("#menu a").each(function(){
  var $anchor = $(this);
  //create an option
  var $option = $("<option></option>");

  //option's value is href of link
  $option.val($anchor.attr("href"));
  //option's text is text of link 
  $option.text($anchor.text());
  //append option to select box
  $select.append($option);
});
Seth Kroger
Seth Kroger
56,413 Points

The convention is to use $ in front of variables that are jquery selectors/objects so it helps you know what they are at first glance. They are a different kind of thing from a string like "Seth". They represent jquery's idea of DOM objects and don't have a straight text/string representation, which is why you use text() to get one.

The starting code uses fullName, which is just a straight string value and doesn't need to be converted to anything else. The code didn't declare a $fullName variable at all.

Oh. I look forward to learning more about the differences in how to handle selectors and strings.

And I see now when I edit the original variable to $fullName it works...

//Show Prompt Window and store value
var $fullName = prompt("What is your full name?");

//Select Input with the id of #fullName

//Insert value in to full name input
$('#fullName').val($fullName);

Thanks for your help