LearnFrenzy
Sign in Take a free test

DOM Manipulation · Predict the Output · Question 12

Appending an element that is already on the page Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 12 Back to Exercise

The page contains this HTML:

<ul id="a"><li>One</li><li>Two</li></ul><ul id="b"></ul>

What does the following JavaScript code print when it runs on this page?

main.js
const a = document.getElementById("a");
const b = document.getElementById("b");
const first = a.firstElementChild;
b.append(first);
b.append(first);
console.log(a.children.length, b.children.length, b.textContent);
  • A 1 1 One Correct
  • B 2 2 OneOne
  • C 2 1 One
  • D 1 2 OneOne
Correct Answer: Option A

A node can be in only one place: append moves it out of #a, and appending it again to the same parent changes nothing. Use cloneNode to copy.

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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links