Skip to content

Instantly share code, notes, and snippets.

@droid001
Created March 10, 2016 07:39
Show Gist options
  • Save droid001/8b68075bd25595a8d16c to your computer and use it in GitHub Desktop.
Save droid001/8b68075bd25595a8d16c to your computer and use it in GitHub Desktop.
Returns all child elements of a given element which match a provided selector
/**
* @param {Element} el
* @param {string} selector
* @return {Element[]}
*/
h.children = function(el, selector) {
var selectors = null,
children = null,
childSelectors = [],
tempId = '';
selectors = selector.split(',');
if (!el.id) {
tempId = '_temp_';
el.id = tempId;
}
while (selectors.length) {
childSelectors.push('#' + el.id + '>' + selectors.pop());
}
children = document.querySelectorAll(childSelectors.join(', '));
if (tempId) {
el.removeAttribute('id');
}
return children;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment