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

How can I select the correct input of a prompt dialog, and set it's value using jQuery?

Hi, complete beginner at jQuery here! Little bit stuck on this code challenge! Would really appreciate any advice on how to solve it! Thanks

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

//Select Input with the id of #fullName
var $select = $("#fullName");

//Insert value in to full name input

$(function () {
  $('#fullName input').val();
});
index.html
<!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>

2 Answers

Emil Rais
Emil Rais
26,873 Points

An input element's value can be retrieved and set using jQuery's val-function:

Retrieve the value by not passing in a value to the function call:

var someName = $("#some-name-field").val();

Set the value by passing in the new value:

$("#some-name-field").val("Mark");

Hi Emil,

Thanks for your answer! Unfortunately, I seem to be still having trouble with it. Not quite understanding what I should place in the "var someName" field. Could you elaborate on this a little for me please?

Much appreciated, Mark

Emil Rais
Emil Rais
26,873 Points

In that particular exercise you're not supposed to retrieve the value that is currently stored in the input field; rather you want to retrieve the input field all together.

var someField = $("#some-field");

After you have retrieved the input field yourself you should set its value to be that of fullName which you prompted the user for.

someField.val(someVariable);