New Metronic Docs!Added Integration Docs with Starter Kits Apps for Laravel, Laravel Livewire, Angular, Vue, Symfony, Blazor Server, Django & Flask & more
Browse Docs

KTDrawer missing target element


How can I get the element that opened the drawer? For KTModal, I use KTModal.getRelatedTarget(), but for drawers the target is always null.


KTDrawer.on("show", async () => {
console.log(KTDrawer.getRelatedTarget());
});


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (3)


Hi,

Let us know if still have the issues.

Thanks



The issue is in the KTDrawer implementation. While the `_relatedTarget` property and `getRelatedTarget()` method exist, the public `toggle()` method doesn't accept a `relatedTarget` parameter, and the `handleToggle()` static method doesn't pass the target element.

Here are your options:

Option 1: Manual Target Setting (Immediate Fix)

// Instead of using data-kt-drawer-toggle attribute
const button = document.querySelector("[data-kt-drawer-toggle="#myDrawer"]");
const drawer = KTDrawer.getInstance(document.querySelector("#myDrawer"));

button.addEventListener("click", (event) => {
drawer.show(event.target); // Pass the target element
});

KTDrawer.on("show", async () => {
console.log(KTDrawer.getRelatedTarget()); // Now returns the button element
});


Option 2: Direct Event Handling

// Get the target element directly from the event
document.addEventListener("click", (event) => {
if (event.target.matches("[data-kt-drawer-toggle]")) {
const drawer = KTDrawer.getInstance(document.querySelector(event.target.getAttribute("data-kt-drawer-toggle")));
if (drawer) {
drawer.show(event.target);
}
}
});


Option 3: Use show() Instead of toggle()

// If you need to manually control the drawer
const drawer = KTDrawer.getInstance(document.querySelector("#myDrawer"));
const button = document.querySelector("#myButton");

button.addEventListener("click", () => {
if (drawer.isOpen()) {
drawer.hide();
} else {
drawer.show(button); // Pass the button as related target
}
});



Thank you. Will the problem be fixed?


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(