Created
December 6, 2021 11:14
-
-
Save aaronsummers/4942e07c547df6cd4d3308de4e97f339 to your computer and use it in GitHub Desktop.
Javascript Document Ready
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
// 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) | |
}); |
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
// 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