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

Code using attr() rather than val() method

This might have already been suggested, I haven't looked through every single post on this particular topic but it's a different approach to the val() method that has been suggested by most.

$("input").attr("value", fullName);

However, it's worth noting that the 'value' attribute of an input element is usually the default. I'm sure others with more jQuery expertise can expand upon this better than I as to whether this is a good or bad method for solving this challenge.

js/app.js
//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
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>

1 Answer

Steven Parker
Steven Parker
229,982 Points

I'm not familiar with '.var()'. But if you mean '.val()', it's essentially just a shorthand for '.attr("value")' and the two can be used interchangably.

Typo (now edited) :) and I appreciate the clarification. I think it's the fact that we don't need to actually select the 'value' attribute for the input element to have it's 'value' changed that seems counter intuitive, at least for me. But I like a good shorthand where it's available.