Update child element recursively in JavaScript

Description

The following code shows how to update child element recursively.

Example


<!--   w  w w  .jav  a  2 s  . c  o  m-->
<html>
<head>
<script type="text/javascript">
function outputNodeProps(nd) {
if (nd.style) {
nd.style.backgroundColor="gray";
}
var children = nd.childNodes;
for(var i=0; i < children.length; i++) {
outputNodeProps(children[i]);
}
}
</script>
<body onload="outputNodeProps(document)">
<div id="div1">
<h1>Header</h1>
<p>P1.</p>
<p>P2</p>
<h2>Header 2</h2>
<p>P1.</p>
<p>P2</p>
</div>
</body>
</html>

Click to view the demo

The code above generates the following result.

Update child element recursively in JavaScript
Home »
  Javascript Tutorial »
    DOM »
      Update
Javascript Tutorial Update
Add and set new attribute for new created H...
Change attribute value for a HTML element i...
Replace a node in JavaScript
Replace the last child in JavaScript
Replace the text for a paragraph in JavaScr...
Set the inner text for a HTML element in Ja...
Set the outer html for a HTML element in Ja...
Update child element recursively in JavaScr...
Update the inner html for a HTML element in...
Use element's inner html to show the result...