0

Is there any way to display all inner html content inside a div with plain javascript?

MyFiddle

<div id="parent">
  <div id="child">
    some-value
  </div>
</div>
<div id="output">

</div>

I am trying to use outerhtml

var parent = document.getElementById("parent");
document.getElementById("output").innerHTML = parent.outerHtml;
2
  • i tried parent.innerHTML but no luck. Commented Nov 21, 2017 at 19:35
  • you must use innerText with outerHTML Commented Nov 21, 2017 at 19:47

3 Answers 3

2

You misspelled outerHTML (HTML in all capital). But to show HTML tags as well, use innerText instead

var parent = document.getElementById("parent");
document.getElementById("output").innerText = parent.outerHTML;
<div id="parent">
  <div id="child">
    some-value
  </div>
</div>
<pre id="output"></pre>

Sign up to request clarification or add additional context in comments.

5 Comments

I am trying to pull html tags also.
Ah, ok... then just swap innerHTML with innerText
Also: to keep whitespace, either use <pre> or the CSS property white-space
Can we do a console.log the html content instead of appending to output element?
Sure. Just remove the second line and use console.log instead
2

Use innerText with outerHTML

var parent = document.getElementById("parent");
document.getElementById("output").innerText = parent.outerHTML;
#output {
  white-space: pre;
}
<div id="parent">
  <div id="child">
    some-value
  </div>
</div>
<div id="output"></div>

1 Comment

Can we do a console.log the html content instead of appending to output element?
1

If you want to create everything from Javascript only. Look at this example.

var div = document.createElement("div");
var nodeDiv = document.createTextNode("Pay attention! This is new.");
div.appendChild(nodeDiv);
var element = document.getElementById("parent");
element.appendChild(div);
<div id="parent">
  <div id="child">
    some-value
  </div>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.