blluch
blluch

Reputation: 23

How to display the outerHTML of an element as a content of another one element?

I want to "print" the result of outerHTML directly to the webpage but it writes the result of outerHTML insted of the "code".

Well, it's easy to show it to the console.log or to the alert window but I want to write it into the page.

document.getElementById("oneId").innerHTML = 
   "The outerHTML of oneId is :" +document.getElementById("anotherId").outerHTML;

The result must be:

The outerHTML of oneId is : <div id="oneId"></div>

Upvotes: 1

Views: 2118

Answers (1)

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67217

You have to use .textContent instead of .innerHTML

document.getElementById("oneId").textContent = 
     "The outerHTML of oneId is :" + document.getElementById("anotherId").outerHTML;

If you use .innerHTML, then the assigning string will be rendered as a html elements not pure text.

Upvotes: 1

Related Questions