Welcome to the Treehouse Community
Looking to learn something new?
Robert Leonardi17,151 Points
Alternative solution to SELECTED
First of all, I deleted ALL files with <li class="selected"> to just <li>
then I added this to app.js These will automatically add the class=selected on menu ul li and will automatically add selected=selected on select option both according to current page
// set pathname let url = window.location.pathname; // cut all the pathname except the file name. let filename = url.substring(url.lastIndexOf('/')+1); // find the li a href = filename and ADD class selected to the li $('#menu li a[href|="'+filename+'"]').parent("li").addClass("selected"); // find the seleect option value = filename and ADD selected to the option $('#menu select option[value|="'+filename+'"]').attr("selected","selected");
Steven Parker215,960 Points
Dynamic handling is not needed for this.
Sure, it would work, but which item should have the selected class would never change for any particular page, so this is an occasion where it makes more sense to include the class directly in the HTML.
However, dynamic handling might come in handy when the control is being used for something other than changing the page location, but in that case you would have an event handler and use the event target to identify the selected item.