Is it possible to achieve this using a class toggle? But don’t know how to create the condition.
Tried using below condition but still did not work as expected.
dmx-class:active=“browserSidemenu.location.pathname.contains(‘order’)”
I’m not sure if it’s possible to use a class toggle, because the script then would override that active toggle. Even if it’s ok I’m also not experienced enough in the front-end to build the condition
I spent a few minutes modifying dmxBootstrap5Navigation.js if you want to try (I assume you’re using Bootstrap 5):
(function() {
// Call init when DOM is ready
if (document.readyState === 'loading') { // Loading hasn't finished yet
document.addEventListener('DOMContentLoaded', _init, { once: true });
} else { // DOMContentLoaded has already fired
_init();
}
function _init() {
// Listen to url changes
window.addEventListener('popstate', _update);
window.addEventListener('pushstate', _update);
// Listen to DOM changes and call update when nodes are added
new MutationObserver(_update).observe(document.body, { subtree: true, childList: true });
// Initial update
_update();
}
function _update() {
var url = window.location.href;
document.querySelectorAll('a.nav-link:not([data-bs-toggle]), a.dropdown-item').forEach(function(elem) {
let match = url.startsWith(elem.getAttribute('dmx-match-href'));
elem.classList.toggle('active', match || elem.href == url || elem.href == url.split("?")[0].split("#")[0]);
});
document.querySelectorAll('a.dropdown-item.active').forEach(function(elem) {
var theItem = elem.closest('.nav-item.dropdown');
if (theItem) {
var theToggle = theItem.querySelector('.dropdown-toggle');
if (theToggle) {
theToggle.classList.toggle('active');
}
}
});
}
})()
And also use the dmx-match-href attribute as exampled in the previous post
I mostly prefer to add the class toggle myself using dmx-class:active="browser1.pathname.startsWith('/order')". It requires the browser component on the page but is very flexible to use and works on all kind of navigation menus, not only bootstrap.
The dmxBootstrap5Navigation was to automate the toggling for you so that it is not needed to add your own logic. This works fine in most cases but will not cover the needs for everyone.
I did copy and pasted the whole code to dmxBootstrap5Navigation.js, but unfortunately still not working. Checking on the console there is no error either. Since 0 knowledge of javascript, I might be lost to understand how to do it.
I put this element inside partials and the browser component are in the main layout.
Somehow I can’t find the browser from the data picker, should I add browser component also inside the partials?
Instead of browser1.pathname should be browser1.location.pathname
I’ve also observed dmxBootstrap5Navigation will remove the active class in this case, making it look like it’s doing nothing. Do you have a way to inhibit this removal just for this nav-link?
Try removing the nav-link class from the link this is something I did worked on one of my menus to stop the auto adding and removing of the active class