Last active
January 30, 2018 17:42
-
-
Save cferdinandi/cdc0ec51ad2d944f537e00fc6a6078ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Listen for all clicks on the document | |
document.addEventListener('click', function (event) { | |
// event.target is the clicked element | |
// If the link has the data-modal selector, we'll use it to toggle our modal | |
// Make sure to include a polyfill for matches(): https://vanillajstoolkit.com/polyfills/matches/ | |
if (event.target.matches('[data-modal]')) { | |
// We can embed the message directly in the modal link if you want using the data-modal attribute | |
// Your link would look like this: | |
// <a data-modal="We want to display this message in the modal. Woohoo!" href="#">Click Me</a> | |
var message = event.target.getAttribute('data-modal'); | |
// Get your modal | |
var modal = document.querySelector('#some-modal'); | |
if (!modal) return; | |
// Set the modal message | |
modal.innerHTML = message; | |
// Then run whatever code you use to open the modal... | |
} | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment