Skip to content

Instantly share code, notes, and snippets.

@NetanelBasal
Created April 8, 2015 12:47
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 NetanelBasal/77c9bde155f3cc91192c to your computer and use it in GitHub Desktop.
Save NetanelBasal/77c9bde155f3cc91192c to your computer and use it in GitHub Desktop.
loop children dom
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div>
<ul>
<li><a href="" class='first'>1</a></li>
<li><a href="">2</a></li>
<li><a href="">3</a></li>
<li><a href="">4</a></li>
</ul>
</div>
</body>
</html>
loopChildren(document.querySelector('div'));
function loopChildren(parent) {
var i = 0;
while(parent.children[i]) {
if(parent.children[i].className === 'first') {
parent.children[i].style.color = 'red';
}
loopChildren(parent.children[i]);
i++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment