Question

Write a simple JavaScript program that will recursively walk the DOM for an associated HTML file ...

write a simple JavaScript program that will recursively walk the DOM for an associated HTML file and print the types of the elements encountered (embedded data need not be printed, we are just interested in the structure), each preceded by an integer reflecting the element’s nesting level within the hierarchical structure of the HTML document. Thus,<HEAD>and <BODY> would be at the second level (‘2’) while <HTML>, would naturally be at the first level (‘1’). You should also print the closing tags for each element found to make the nesting clear. Thus, your output should look something like the following:

1 <HTML>

2<HEAD>

  element types in “<HEAD></HEAD>” labelled appropriately

2 </HEAD>

2<BODY>

    element types in “<BODY></BODY>” labelled appropriately

2</BODY>

1</HTML>

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Let's say this following is our html code..

<!doctype html>
       <HTML>
       <HEAD>
       </HEAD>
       <BODY>
           <P> Top 3 Channel 3 Thai actresses: </P>
           <ol>
           <li>U</li>
           <li>V</li>
           <li>B</li>
           </ol> 
         <div id="idDiv"></div>
         <script type="text/javascript" 
          src="script.js"> 
         </script>
      <BODY>
      </HTML>

JavaScript code:

var element = document.getElementById("idDiv");

function printNodeInfo(node) {
  if (node.nodeType == Node.TEXT_NODE && !node.nodeValue.trim()) {
    return;
  }
  element.innerHTML += node;
  element.innerHTML += ", nodeName: " + node.nodeName;
  element.innerHTML += ", nodeType: " + node.nodeType;
  element.innerHTML += ", innerHTML: " + node.innerHTML;
  element.innerHTML += "<br>";
}

// Get a handle of the ordered-list node.
var orderedlistNode = document.getElementsByTagName("ol")[0];
theDOMElementWalker(orderedlistNode);

function theDOMElementWalker(node) {
  if (node.nodeType == 1) {
    node = node.firstChild;

    while (node) {
      theDOMElementWalker(node);
      printNodeInfo(node);
      node = node.nextSibling;
    }
  }
}
element.innerHTML += "<br>";
Add a comment
Know the answer?
Add Answer to:
Write a simple JavaScript program that will recursively walk the DOM for an associated HTML file ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • write an html file and a JavaScript file to show the due date of a book...

    write an html file and a JavaScript file to show the due date of a book checked out from a library. The html file should contain necessary elements to work with the JavaScript file described as follow. The JavaScript file needs to meet the following requirements: 1. It needs to have an object called “book” that have attributes called name, title, author and price. The values of these attributes are then written to the html file using JavaScript statements. 2....

  • Javascript to edit a HTML file. Where to begin? <!doctype html> <html>    <head>      <meta charset="utf-8">      <title>TODO:...

    Javascript to edit a HTML file. Where to begin? <!doctype html> <html>    <head>      <meta charset="utf-8">      <title>TODO: Change The Title</title>    </head>    <body>      <h1></h1>      <p>First paragraph.</p>      <p class="intro">Second <span>paragraph</span>.</p>      <button id="start">Start</button>      <main> </main>      <!--       Test 6: DOM Programming       You MAY consult the notes for week 7, but you MAY NOT discuss this test or       your code with any other students.  All work must be your own.       Instructions:              1. Put all solution code in a file named solution.js.  When you upload your          solution to...

  • Using HTML and JavaScript. For this assignment you should submit a single zip file that contains...

    Using HTML and JavaScript. For this assignment you should submit a single zip file that contains the following two files: index.html script.js index.html should be a skeleton HTML page. So it should have the following tags: doctype html head meta title body script If you were to open index.html without including the associated JavaScript it should be entirely blank. You should then use JavaScript to create all of the content of this page and append it to the body of...

  • Need help with creating this JavaScript file. I'm not sure where or how to begin. 1. You are working solely with the DOM object. Thus, you should be coding primarily in the js file. You must use...

    Need help with creating this JavaScript file. I'm not sure where or how to begin. 1. You are working solely with the DOM object. Thus, you should be coding primarily in the js file. You must use the addEventListener, in addition you may need to use id and class attributes as needed. The .html and .css files have been provided to you. See the .png files for an example of what is expected. 2. Place the javaScript file in its...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT