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

HTML jQuery Basics (2014) Creating a Mobile Drop Down Menu Using parent(), hasClass() and prop()

Shariq Shaikh
Shariq Shaikh
13,945 Points

I'm still having trouble understanding how jquery prop method works, can anyone explain?

I get how the selected being used in the if statement is different from the selected from in the prop method call, what i son't get is how the true parameter causes the current page to be page listed in the drop down menu

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Shariq,

The jQuery prop method is special as it has some magic working in our favor which is why it's confusing when you see true been used as we know that's not valid and selected should be used, in the magical world of prop there is a set of expressions that jQuery is looking for and if it finds one that can be translated to a boolean it will then check the status of that attribute on the element and set it according to whatever value we have defined.

In the case of this challenge the attribute selected is a special keyword jQuery is looking for so when we write prop('selected', true) we're telling jQuery to toggle the selected state of the option element to on whereas prop('selected', false) will remove the selected attribute.

Before jQuery 1.6 this wasn't an option so we had to use the attr method but now day's it's hard to find a plugin not using this faster technique, you can read more about prop at the below link.

http://api.jquery.com/prop/