Skip to content

Instantly share code, notes, and snippets.

@lordfriend
Created October 1, 2014 12:51
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 lordfriend/6c224b777fcd594bae34 to your computer and use it in GitHub Desktop.
Save lordfriend/6c224b777fcd594bae34 to your computer and use it in GitHub Desktop.
Use with event delegate when jquery is not available.
/**
* filter the event target for the nya-bs-option element.
* Use this method with event delegate. (attach a event handler on an parent element and listen the special children elements)
* @param target event.target node
* @param parent {object} the parent, where the event handler attached.
* @return the filtered target or null if no element satisfied the selector.
*/
var filterTarget = function(target, parent) {
var elem = target,
className;
if(target == parent) {
return null;
} else {
do {
className = ' ' + elem.className + ' ';
if(elem.nodeType === 1 && className.replace(/[\t\r\n\f]/g, ' ').indexOf('nya-bs-option') >= 0) {
return elem;
}
} while((elem = elem.parentNode) && elem != parent && elem.nodeType !== 9);
return null;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment