Skip to content

Instantly share code, notes, and snippets.

@aaronsummers
Created December 6, 2021 11:14
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 aaronsummers/4942e07c547df6cd4d3308de4e97f339 to your computer and use it in GitHub Desktop.
Save aaronsummers/4942e07c547df6cd4d3308de4e97f339 to your computer and use it in GitHub Desktop.
Javascript Document Ready
// https://stackoverflow.com/questions/45291962/vanilla-js-version-of-jquery-document-on-click-for-links#answer-45292145
function addEvent(parent, evt, selector, handler) {
parent.addEventListener(evt, function(event) {
if (event.target.matches(selector + ', ' + selector + ' *')) {
handler.apply(event.target.closest(selector), arguments);
}
}, false);
}
/* To be used as */
addEvent(document, 'click', 'a[href]', function(e) {
console.log(this)
});
// https://stackoverflow.com/questions/45291962/vanilla-js-version-of-jquery-document-on-click-for-links#answer-45292145
document.addEventListener('click', function(event) {
if (event.target.matches('a[href], a[href] *')) {
event.preventDefault();
console.log('works fine')
}
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment