Skip to content

Instantly share code, notes, and snippets.

@mcfedr
Last active August 29, 2015 14:11
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 mcfedr/bade9d5e823895685a1c to your computer and use it in GitHub Desktop.
Save mcfedr/bade9d5e823895685a1c to your computer and use it in GitHub Desktop.
Adding events in different places
[].forEach.call(document.querySelectorAll('td[data-href]'), function(el) {
el.addEventListener('click', function (event) {
window.document.location = this.getAttribute('data-href');
});
});
$('td[data-href]').on('click', function() {
window.document.location = $(this).attr('data-href');
});
/*
This method as the advantage that if you add new elements to the document the event listener
will still work
*/
document.addEventListener('click', function(e) {
if (e.target.tagName === 'TD' && e.target.hasAttribute('data-href')) { // For historical reason tagName is always uppercase
window.document.location = e.target.getAttribute('data-href');
}
});
$(document).on('click', 'td[data-href]', function() {
window.document.location = $(this).attr('data-href');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment