Skip to content

Instantly share code, notes, and snippets.

@Kelderic
Created August 4, 2017 13:10
Show Gist options
  • Save Kelderic/432269dbfb3316f4e19669fe99378bf2 to your computer and use it in GitHub Desktop.
Save Kelderic/432269dbfb3316f4e19669fe99378bf2 to your computer and use it in GitHub Desktop.
Sometimes I'll create list items with nested child elements. A click's event.target may register on the nested element or the parent. This element prototype allows standardization of that.
HTMLElement.prototype.getAncestorByTagName = function( tagName ) {
var target = this;
while ( target.tagName.toLowerCase() != tagName && target.tagName.toLowerCase() != 'body' ) {
target = target.parentNode;
}
if ( target.tagName.toLowerCase() == 'body' ) {
return false;
} else {
return target;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment