Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dmhall0's avatar

Pass Modal Name as Variable to EventListener

Using Livewire. I have several modals on my dashboard and trying to close them by $this->dispatchBrowserEvent('closeModal', ['modalName' => '#moodModal']); to a

<script type="text/javascript">
    window.addEventListener('closeModal', () => {
        $(.modalName).modal('hide');
    });
</script>

If I use the actual modalName then it works. I assume I am not passing my variable correctly. Any help would be greatly appreciated! Thank you in advance.

0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

You need to capture the event and then access the detail within it. I think like

<script type="text/javascript">
    window.addEventListener('closeModal', event => {
        $(event.detail.modalName).modal('hide');
    });
</script>

Please or to participate in this conversation.