DeveloperBreeze

Custom Events Development Tutorials, Guides & Insights

Unlock 1+ expert-curated custom events tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your custom events skills on DeveloperBreeze.

Tutorial
javascript php

Managing Modals with Livewire and JavaScript

Add a script to listen for the custom events emitted by Livewire and open the modals accordingly.

<script>
    document.addEventListener('DOMContentLoaded', function () {
        // Listen for the custom 'open-modal' event
        window.addEventListener('open-modal', event => {
            const modalId = event.detail.modalId;
            const modalElement = document.getElementById(modalId);
            if (modalElement) {
                modalElement.classList.remove('hidden');
            }
        });

        // Close modal logic
        document.querySelectorAll('[data-modal-hide]').forEach(button => {
            button.addEventListener('click', function () {
                const modalId = this.getAttribute('data-modal-hide');
                const modalElement = document.getElementById(modalId);
                if (modalElement) {
                    modalElement.classList.add('hidden');
                }
            });
        });
    });
</script>

Aug 14, 2024
Read More