Get the whole innerHTML (parent + child)
Asked Answered
M

3

8

On a js script, I have a div which I add dynamically some childs. So from the beginning, I can't know how many childs my div will contains.

When I try to get the innerHTML of my div container, I only obtains the html of the div. Not the html of the node.

How can I get the innerHTML of the whole components, through the childs ?

Midwinter answered 13/1, 2016 at 7:36 Comment(0)
D
11

Try this:

document.getElementById('MyId').outerHTML

This gets all the html of the element, inside and itself.

Destruct answered 13/1, 2016 at 7:38 Comment(0)
S
9

enter image description here


.outerHTML for outer, .innerHTML for inner.

Sustenance answered 13/1, 2016 at 7:57 Comment(0)
C
2

Using outerHTML will do the trick.

document.getElementById('YourId').outerHTML
Cinelli answered 13/1, 2016 at 7:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.