How to get to the far parent in JS? To make this code look more cool.
for(var i=0; i < carlingFox.length; ++i){ var el = sexturyFox[i].parentNode.parentNode.parentNode.parentNode el.style.background = 'red' }
How to get to the far parent in JS? To make this code look more cool.
for(var i=0; i < carlingFox.length; ++i){ var el = sexturyFox[i].parentNode.parentNode.parentNode.parentNode el.style.background = 'red' }
function closest(el, cl) { var elem = el; while(elem.className != cl) { if(elem.tagName.toLowerCase() == 'html') return false; elem = elem.parentNode; } return elem;
}
Arguments ate - element, cl - the class of the element to be found. If the house does not have an email with this class, returns false;
You can write a function like:
function gt(node, indx) { var parent; for (var i = 0; i <= indx; i++) { node = node.parentNode; } return(node); }
and call by index (0 is the first parent)
gt(sexturyFox[i], 3).style.background = 'red';
Source: https://ru.stackoverflow.com/questions/133147/
All Articles