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());
});
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
});
// 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);
}
}
});
// 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?