Last active
August 29, 2015 14:11
-
-
Save mcfedr/bade9d5e823895685a1c to your computer and use it in GitHub Desktop.
Adding events in different places
This file contains hidden or 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
[].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