Skip to content Skip to sidebar Skip to footer

Expand Parent Menu If Child Menu Is Selected

I have a menu with sub child menus. The problem I am facing is that whenever I select a child menu after page loads, the menu collapses. I want to open the parent menu expanded. Th

Solution 1:

You have the full, absolute URL's in the links; that can be used to your advantage. Simply use document.querySelector with the square bracket selector, and the location.href property:

document.querySelector("a[href='" + location.href + "']").classList.add("selected");

Then, do whatever you need to do to it. In this example, I'm adding the selected class.

Fiddle

Post a Comment for "Expand Parent Menu If Child Menu Is Selected"