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 trialMark Conway
1,215 PointsHow 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
//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();
});
<!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
26,873 PointsAn 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");
Mark Conway
1,215 PointsHi 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
26,873 PointsIn 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);