I’m trying to reuse this code from the theme to create a popup, it uses this custom element. When I try to console log the value of the attribute it returns null but if I console log the value of “this”, it returns the custom element. Any ideas why? Appreciate if someone can help.
Below is the code:
HTML:
<modal-opener data-modal="#PopupModal-custom" class="product-popup-modal__opener no-js-hidden
quick-add-hidden ">
<button id="ProductPopup-custom" class="product-popup-modal__button link"
type="button" aria-haspopup="dialog">Popup!!</button>
</modal-opener>
JS:
class ModalOpener extends HTMLElement {
constructor() {
super();
const button = this.querySelector('button');
if (!button) return;
button.addEventListener('click', () => {
const modal = document.querySelector(this.getAttribute('data-modal'));
// Below returns null but if I print value of this, it returns the modal-opener tag
console.log(modal);
if (modal) modal.show(button);
});
}
}