Skip to content

Instantly share code, notes, and snippets.

@NightZpy
Last active January 28, 2017 07:04
Show Gist options
  • Save NightZpy/89ba7940033b306d82c32a858f1cc6e7 to your computer and use it in GitHub Desktop.
Save NightZpy/89ba7940033b306d82c32a858f1cc6e7 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test page</title>
</head>
<body>
<div class="a">
a
<div class="b">b</div>
</div>
<script>
function appendChildren() {
var allDivs = document.getElementsByTagName("div");
var cDivs = allDivs.length;
for (var i = 0; i < cDivs; i++) {
var currentDiv = allDivs[i];
var childs = currentDiv.getElementsByTagName("div");
if (childs.length > 0)
innerDivs(childs);
var newDiv = document.createElement("div");
currentDiv.appendChild(newDiv);
}
}
function innerDivs (divs) {
var cDivs = divs.length;
for (var i = 0; i < cDivs; i++) {
var currentDiv = divs[i];
var childs = currentDiv.getElementsByTagName("div");
alert(currentDiv.innerHTML);
if (childs.length > 0)
innerDivs(childs);
var newDiv = document.createElement("div");
//decorateDiv(newDiv);
currentDiv.appendChild(newDiv);
}
}
// Mock of decorateDiv function for testing purposes
function decorateDiv(div) {
}
appendChildren();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment