Super Sale Limited Time 50% OFF for All-Access Plans
Save 50% Now

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
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • 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
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(