Question

Question: 3 Consider the following code segment: <!DOCTYPE html> <html> <body> <ul> <li id = "list-one">Homework</li> <li>Walk the Dog</li> <li>Feed Cats</li> <li>Water Plants</li> </ul> </body> </html> Which of the following code segments would successfully access the list item with the id “list-one”? A. document.getElementById("list-one"); B. document.getElementsByTagName("li")[1]; C. document.getElementsByTagName("ul")[0].firstElementChild; A only A and B only A and C only B and C only A, B and C

Answers

Answered by GPT-4o mini
The correct answer is **A only**.

Here's the reasoning:

- **A. `document.getElementById("list-one");`**: This code correctly accesses the list item with the id “list-one”. It directly retrieves the element by its ID.

- **B. `document.getElementsByTagName("li")[1];`**: This accesses the second `<li>` element in the list (index 1 corresponds to "Walk the Dog", which does not have the id "list-one").

- **C. `document.getElementsByTagName("ul")[0].firstElementChild;`**: This accesses the first child of the first `<ul>` element, which is again the `<li>` element with the text "Homework", but it does not retrieve it by its id.

Thus, only option A successfully accesses the element by its ID.

Related Questions