nerdexam
Microsoft

98-382 · Question #29

You are designing a web page that contains a list of animals. The web page includes a script that outputs animals from a list. You create the following HTML to test the script: ``html <p>Animals</p>…

``javascript function showList() { var list = document.getElementsByTagName("li"); for (var i = 0; i < list.length; i++) { document.getElementById("list").innerHTML += list[i].innerHTML + "<br>"; } } ``

Working with the Document Object Model (DOM)

Question

You are designing a web page that contains a list of animals. The web page includes a script that outputs animals from a list. You create the following HTML to test the script:
<p>Animals</p>
<ul>
 <li>Dog</li>
 <li>Cat</li>
 <li>Lion</li>
</ul>
<p>Click the button to display the animals.</p>
<button onclick="showList()">Show List</button>
<div id="list"></div>
You need to create a function that will display the list of animals, including any formatting, in the div element. How should you complete the code? To answer, select the appropriate code segments in the answer area. NOTE: Each correct selection is worth one point.

Explanation

function showList() {
  var list = document.getElementsByTagName("li");
  for (var i = 0; i < list.length; i++) {
    document.getElementById("list").innerHTML += list[i].innerHTML + "<br>";
  }
}

Topics

#DOM manipulation#innerHTML#getElementById#ul/li elements

Community Discussion

No community discussion yet for this question.

Full 98-382 Practice