LearnFrenzy
Sign in Take a free test

DOM Manipulation · Predict the Output · Question 2

textContent and innerHTML of nested markup Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

The page contains this HTML:

<p id="msg">Hello <b>World</b>!</p>

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

main.js
const msg = document.getElementById("msg");
console.log(msg.textContent);
console.log(msg.innerHTML);
  • A Hello <b>World</b>! Hello World!
  • B Hello World! Hello World!
  • C Hello World! Hello <b>World</b>! Correct
  • D Hello ! Hello <b>World</b>!
Correct Answer: Option C

textContent joins all the text of the descendants; innerHTML serialises the markup.

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