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 trialjinhwa yoo
10,042 Pointshelp me
I need to understand this part....
can you explain concretely?????
//cycle over menu links
$("menu a").each(function(){
var $anchor = $(this);
//create an option
var $option = $("<option></option>");
//option's value is the href
$option.val($anchor.attr("href"));
//option's text is the text of link
$option.text($anchor.text());
$select.append($option);
});
//create button
2 Answers
Dana Al Taher
Courses Plus Student 9,499 PointsThe first part of the code (which is cycling over the menu links) applies the function given in the statement to each and every anchor (link) in the menu.. This is using the "each()" method which applies whatever follows it to each of the specified items before it.
The function isn't closed but continues to the second part, which sets the value of the "href" attribute as the value of the "value" attribute in each of the "option" tags appended in the end to select which is the dropdown menu. It also sets the text of the anchor (link/ a) element as the text of the option element.
In other words, this code starts with selecting each link in the menu and applying the function. When the function is applied, a variable ($anchor) is created to reference each link one at a time (since the function repeats with each and every link) and another variable ($option) is created to reference the option tag which will be added to the code through jQuery in the end. After that, the value of the "value" attribute in option tags is set to the value of the "href" attribute in anchor elements so that each option in the select menu gets a unique value (which you will later on use to specify as the url you redirect the user to after selecting any option as it is obviously the path to your selected page to visit). Then, the text of the link (which is the text displayed for the user to click on in the normal menu) is set to the text of the option so that the user can see it in the menu and click on it to select it. Finally, the option tag is inserted to the select menu using the "append()" method in order for it to appear in the browser..
I hope this helps :)
Tal Mantelmakher
3,316 PointsLooks like the code is loading the menu you have ("menu a") into a select with its respective text and href values.
For each menu link there will be added an option to the select.
jinhwa yoo
10,042 Pointsjinhwa yoo
10,042 Pointseach option in the select menu gets a unique value (which you will later on use to specify as the url you redirect the user to after selecting any option as it is obviously the path to your selected page to visit). Then, the text of the link (which is the text displayed for the user to click on in the normal menu) is set to the text of the option so that the user can see it in the menu and click on it to select it. Finally, the option tag is inserted to the select menu using the "append()" method in order for it to appear in the browser..
this part is hard to understand... plz.. give me an example...
Dana Al Taher
Courses Plus Student 9,499 PointsDana Al Taher
Courses Plus Student 9,499 PointsSo basically you have a link in the menu list which contains the the list items you have as buttons. Each link obviously has an "href" attribute because, what's the purpose of adding a link if you don't have an actual link you want to redirect the user to?
In your jQuery file, there is a function that applies whatever is inside it to each and every anchor element. So for each anchor element, an option element is created and has the properties given to it in the fuction.
In the first part of the function, we set "the link inside the anchor element" as the "value of the value attribute of the option element (which is inside the select menu)." Each option element in a select menu has a specific value attribute which differentiates it from the rest of the options. Usually, programmers use the value attribute to be able to specify specific styles and other properties to the chosen option... For example, this code:
returns this select menu:
The second part states that each option gets specific text based on the text of one of the links. For instance, this code:
returns this code:
The final part just puts everything together with the append() method used in:
$select.append($option);
as this part of code selects the "$select" variable, which is the select menu, and adds the option (with all of its new properties - the value attribute and the text) to the menu.