Skip to content

Instantly share code, notes, and snippets.

@nilliams
Last active December 27, 2015 17:19
Show Gist options
  • Save nilliams/7361860 to your computer and use it in GitHub Desktop.
Save nilliams/7361860 to your computer and use it in GitHub Desktop.
A relatively sane onClick helper (Webkit only), for e.g. Node-Webkit work. Add additional prefixed versions of `matchesSelector` for further browser compat.
function onClick(selector, fn) {
document.addEventListener('click', function(e) {
if ( _matchesSelector(e.target, selector) ) fn(e);
}, false);
}
function _matchesSelector(el, selector) {
var doc = document.documentElement;
var fn = doc.matchesSelector || doc.webkitMatchesSelector;
return fn.call( el, selector );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment