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 trialIliya Narsiya
3,387 PointsMy question about method prop()
var $select = $("<select></select>");
$("#menu").append($select);
$("#menu a").each(function(){
var $anchor = $(this);
var $option = $("<option></option>");
if ($anchor.parent().hasClass("selected") ){
$option.prop("selected",true);
}
$option.val($anchor.attr("href"));
$option.text($anchor.text());
$select.append($option);
});
var $button = $("<button>Go</button>");
$("#menu").append($button);
$button.click(function(){
window.location = $select.val();
});
Why here in the part code stated value true?
$option.prop("selected",true);
2 Answers
Greg Kaleka
39,021 PointsHi Iliya,
I'm not sure I completely understand your question, but you can find the documentation for the prop()
method, and this specific version of it here. Essentially, you're setting the property "selected" to true
. If you put false
in there, it would remove the property "selected" from the html.
Here's the javascript and resultant html:
$option.prop("selected", true);
leads to:
<option selected></option>
whereas:
$option.prop("selected", false);
leads to:
<option></option>
Let me know if this answers your question!
-Greg
Iliya Narsiya
3,387 PointsBut if remove value true, code will be work also .
Iliya Narsiya
3,387 Pointsthank you very much for your help!
Niclas Valentiner
8,947 Points$option.prop("selected",true); This sets the selected property of $option to true.
Iliya Narsiya
3,387 Pointswhy he is set?
Ryan Field
Courses Plus Student 21,242 PointsIt is being set because that is how you make a certain option selected in a dropdown (or select
menu). If you are unaware of how such menus work, there is more information here.
Iliya Narsiya
3,387 PointsRyan Field thank you! Now i all understood!
Greg Kaleka
39,021 PointsGreg Kaleka
39,021 PointsBy the way, I edited your post to add in code formatting. Take a look at how to do this yourself: click on the button below your post and click edit. You'll see the markup I added.