KTDrawer no target element
KTDrawer does not have getTargetElement like ktmodal
modal.on("show", function () {
console.log(modal.getTargetElement());
}); Replies (8)
Hi
Sorry about that. getRelatedTarget() returns null when used inside the show event because the related target is set after that event fires. We’ve noted this for a future release.
Workaround 1 – use the shown event
By the time shown runs, the related target is available:
drawer.on("shown", function () {
console.log(drawer.getRelatedTarget()); // returns the element that triggered the drawer
}); Workaround 2 – if you must use show
If you need the trigger inside show, use a delegated click handler to track it:
let lastToggleElement = null;
document.body.addEventListener("click", function(e) {
const toggle = e.target.closest("[data-kt-drawer-toggle]");
if (toggle) lastToggleElement = toggle;
}, true);
drawer.on("show", function () {
const trigger = lastToggleElement || drawer.getRelatedTarget();
console.log(trigger);
}); Using shown is usually enough since it runs once the drawer is open. We’ll consider fixing the timing and adding getTargetElement() in a future release.
Hi
KTDrawer doesn’t expose getTargetElement(), but it does expose getRelatedTarget(), which returns the element that triggered the drawer (the same idea as KTModal’s target). You can use it like this:
drawer.on("show", function () { console.log(drawer.getRelatedTarget());}); We’ve noted the request for a getTargetElement() method to be consistent with KTModal and will consider adding it in a future release.
Thank you
Hi
Sorry for the earlier mix-up. There’s no update yet. KTDrawer still doesn’t expose a getTargetElement() method like KTModal. We’ve noted the request and will consider adding it in a future release. I appreciate your patience.