Assuming a page with four paragraph tags, what is the correct code to change the content of the second paragraph?

Prepare for the Web Development 201 Test. Study with flashcards, multiple-choice questions, and detailed explanations. Get exam-ready!

The correct code to change the content of the second paragraph is indeed represented in the first choice. Using document.getElementsByTagName('p')[1].innerHTML = "What does the Fox say?" effectively targets the second paragraph element on the page.

Here's how it works:

  1. The document.getElementsByTagName('p') method retrieves a live HTMLCollection of all paragraph elements (<p>) present in the document.

  2. This collection is indexed, starting from 0, meaning the first paragraph is accessed with an index of 0, and the second paragraph is accessed with an index of 1.

  3. By appending [1], we specifically select the second paragraph.

  4. The .innerHTML property allows us to modify the content (HTML markup) inside that paragraph tag. Therefore, assigning a new string to innerHTML replaces the existing content of the second paragraph with "What does the Fox say?"

In contrast, the other choices do not successfully achieve the intended task for several reasons. One involves using the getElementById method, which looks for an element with a specific ID rather than index position. This would only succeed if a paragraph had an

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy