Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
Last active January 30, 2018 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cferdinandi/cdc0ec51ad2d944f537e00fc6a6078ae to your computer and use it in GitHub Desktop.
Save cferdinandi/cdc0ec51ad2d944f537e00fc6a6078ae to your computer and use it in GitHub Desktop.
// 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