Get 2024 Templates Mega Bundle!14 Bootstrap, Vue & React Templates + 3 Vector Sets
Get for 99$

KTMenu.updateByLinkAttribute not setting links in kt_header_user_menu_toggle


I'm using KTMenu.updateByLinkAttribute in my vanilla html project powered by htmx, I'm expecting
```

// This will recalculate and update the menu
htmx.onLoad(function(evt) {
KTMenu.updateByLinkAttribute(window.location.pathname, "hx-get");
});

```

to update all menus, but it's only updating my sidebar menu.

the problem is with this block
```
// Render menus by url
KTMenu.updateByLinkAttribute = function(value, name = "href") {
// Set menu link active state by attribute value
var elements = document.querySelectorAll('[data-kt-menu="true"]');

if ( elements && elements.length > 0 ) {
for (var i = 0, len = elements.length; i < len; i++) {
var menu = KTMenu.getInstance(elements[i]);

if (menu) {
var link = menu.getLinkByAttribute(value, name);
if (link) {
menu.setActiveLink(link);
}
}
}
}
}
```

it only updates for
```
if (link) {
menu.setActiveLink(link);
}
```

but my user dropdown don't have link to "/" but it have link to "/profile" when I click "/profile" it's set as active and when I go back to "/" the sidebar updates but the user menu link "/profile" stays active.


Text formatting options
Submit

Replies (1)


Hi,

Can you please make sure you are calling KTMenu.updateByLinkAttribute(window.location.pathname, "hx-get") after your path (window.location.pathname) changes?

The logic of KTMenu.updateByLinkAttribute is to set all menu links as active that match the criteria, the attribute and it's value. In your case "/" and "/profile" are 2 different values.

Regards.


Text formatting options
Submit
Text formatting options
Submit