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 trialRobert Leonardi
17,151 PointsAlternative 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");
1 Answer
Steven Parker
231,275 PointsDynamic 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.
Robert Leonardi
17,151 PointsRobert Leonardi
17,151 PointsI think that make sense I hate it when I overdo something hahaha thanks for valuable input