Skip to content

Instantly share code, notes, and snippets.

@frippz
Last active May 3, 2019 06:00
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 frippz/9863552973bbe240ab1f5b84255464ce to your computer and use it in GitHub Desktop.
Save frippz/9863552973bbe240ab1f5b84255464ce to your computer and use it in GitHub Desktop.
Mocking user actions with buttons
/**
* prototypeButtons
*
* Listen for click event across the document & trigger on
* buttons that have a data-prototype-url attribute.
*
* Usage:
* <button type="button" data-prototype-url="page.html">Click me</button>
*
* Do NOT use this in production. That’s what links are for.
*/
'use strict';
(function () {
document.addEventListener('click', function (event) {
if (event.target.matches('[data-prototype-url], [data-prototype-url] *')) {
var button = event.target.closest('[data-prototype-url]');
var buttonURL = button.getAttribute('data-prototype-url');
window.location.href = buttonURL;
console.log(button.textContent + ' clicked');
}
}, false);
})();
@frippz
Copy link
Author

frippz commented May 3, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment