Question 13
Back to Exercise
The page contains this HTML:
<div id="wrap"><p id="p">P</p></div>What does the following JavaScript code print when it runs on this page?
const p = document.getElementById("p");
p.insertAdjacentHTML("beforebegin", "<i>1</i>");
p.insertAdjacentHTML("afterbegin", "<i>2</i>");
p.insertAdjacentHTML("beforeend", "<i>3</i>");
p.insertAdjacentHTML("afterend", "<i>4</i>");
console.log(document.getElementById("wrap").innerHTML);
- A <i>1</i><p id="p"><i>2</i>P<i>3</i></p><i>4</i> Correct
- B <p id="p"><i>1</i><i>2</i>P<i>3</i><i>4</i></p>
- C <i>1</i><i>2</i><p id="p">P</p><i>3</i><i>4</i>
- D <i>2</i><p id="p"><i>1</i>P<i>4</i></p><i>3</i>
Correct Answer: Option A
beforebegin/afterend go outside the element (as siblings); afterbegin/beforeend go inside it, at the start and end.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!