Super Sale Limited Time 50% OFF for All-Access Plans
Save 50% Now

Set menu active javascript


Hello I just bought Good Theme html5 version, but can't figure out how to set automatically left menu items as "active" on every page load, maybe through a javascript. Can you help?


Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (4)

Deleted comment

thanks a lot



Can I have a JS anyway?



Can you please replace core/html/src/js/components/menu.js with and recompile your assets with gulp or webpack then you can use the KTMenu's new API method as:


var activeLink = menu.getLinkByAttribute("/users/group/add");
menu.setActiveLink(activeLink);


Or globally call the below API method to automatically set an active menu link based on the current page URL:


KTMenu.updateByLinkAttribute("/users/group/add");


Regards.



Hi,

Usually, this is handled in the server-side code by adding required classes to the active menu link and its parents as you can see in this examples.

If you use JS to set the active menu link, it can be noticeable that the menu active item is set after page load and the delay can be 2-3seconds. If you use the server-side approach it will be an instance with the page load.

Regards.


Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(