Event Listener Development Tutorials, Guides & Insights
Unlock 3+ expert-curated event listener tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your event listener skills on DeveloperBreeze.
Adblocker Detected
It looks like you're using an adblocker. Our website relies on ads to keep running. Please consider disabling your adblocker to support us and access the content.
Tutorial
javascript php
Managing Modals with Livewire and JavaScript
<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>- Custom Events: We use Livewire's
dispatchBrowserEventto emit a custom eventopen-modalwith themodalIdas a parameter. - JavaScript Listener: A listener is set up for the
open-modalevent to open the specified modal by removing thehiddenclass. This ensures that JavaScript functions properly even after Livewire re-renders parts of the page. - Close Logic: We add listeners for buttons that hide the modals, ensuring a full modal lifecycle.
Aug 14, 2024
Read More