LearnFrenzy
Sign in Take a free test

DOM Manipulation · Predict the Output · Question 14

Shallow and deep clones Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 14 Back to Exercise

The page contains this HTML:

<div id="card" class="card"><h3>Quiz</h3></div>

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

main.js
const card = document.getElementById("card");
const shallow = card.cloneNode();
const deep = card.cloneNode(true);
console.log(`[${shallow.textContent}]`, shallow.className, deep.textContent, deep.isConnected);
  • A [] card Quiz false Correct
  • B [Quiz] card Quiz true
  • C [] undefined Quiz false
  • D [Quiz] card Quiz false
Correct Answer: Option A

cloneNode() copies the element and its attributes only; cloneNode(true) copies the children too. Clones are not on the page until you insert them.

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