Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript jQuery Basics (2014) Creating a Mobile Drop Down Menu Perform: Part 3

Seth Warner
Seth Warner
5,348 Points

Can someone tell me if Im correct, I got a little lost towards the end..

He lost me completely right when he finished with the CSS. I understood everything he did in the CSS but when he started with "if" I was lost. So let me explain what I think is going on with everything.

By looking at the code; we are appending $select to the menu class. The $option variable is appended to the $select variable.

The $option variable is wrapping each of the menus attributes in option tags. The $option tags values are the menu classes attributes values and the option tags texts are the menu classes attributes texts.

We are appending the $button variable, thats just a button with the word go in it, to the menu class as well.

In CSS we are technically showing both the drop down and nav bar at the same time. CSS will hide one or the other depending on what screen size is being used. If it's out of the specified range, in our case this would be the iPhone, it would chose to hide the smaller media query out of the two. If it is small then it would choose to hide the larger, in our car the regular webpage nav bar out of the two.

What on EARTH went on in the part 3 video?! lol please help me..I have NO idea what this is...

if$($anchor.parent.hasclass("selected")) {
 $option.prop("selected", true); 
var $select = $("<select></select>");
 $(#menu a).append($select);

 $("#menu").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 a").append($button);

 $button.click(function(){
 window.location = $select.val();
})

})

2 Answers

John Coolidge
John Coolidge
12,614 Points

Hello Seth,

Given the following code:

if ($anchor.parent.hasclass("selected")) {
   $option.prop("selected", true);
}

If the link's parent has the class "selected", then the option's property will be changed to selected. (You can view this in the HTML with the Chrome Dev Tools). When an option's "selected" property is true, this means that when the new page loads, the About page, for example, then when you look up at the menu on your iPhone, you will see that the menu page, in this case, the About page, and the option in the select menu will be the same. I can tell what page I'm on without ever looking at the page's content, because the select menu will show me which page I'm on. If I'm on the About page, "About" will be the option that I can see in the menu. If I'm on the Home page, "Home" will be the option that I can see in the menu.

I hope that helps answer your question. If I've missed something, please respond and I'll try to clarify.

John

Seth Warner
Seth Warner
5,348 Points

Awesome @John Coolidge That dose help. I ended up figuring it out a day or so ago but running through your information did give me more clarification, much appreciated!

Seth

jason chan
jason chan
31,008 Points
 var $button = $("<button>Go</button>")

You forgot quotes.