Skip to content

Instantly share code, notes, and snippets.

@jmlavoier
Created June 27, 2017 21:01
Show Gist options
  • Save jmlavoier/567b391679fb5ccddb174df7431a8ca1 to your computer and use it in GitHub Desktop.
Save jmlavoier/567b391679fb5ccddb174df7431a8ca1 to your computer and use it in GitHub Desktop.
08-challenge-code
const p = document.querySelector('.p1');
const parents = function (el) {
let arrayElements = [];
let currentEl = el;
return function findParentByTagName(tn) {
currentEl = currentEl.parentElement;
if (!currentEl) return arrayElements;
if (currentEl.tagName === tn) {
arrayElements.push(currentEl);
}
return findParentByTagName(tn);
}
}
//Parents
const getParentsByTagName = parents(p);
console.log(getParentsByTagName('DIV')); // [div#cont.content, div, div]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment