Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the whole innerHTML (parent + child)

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 ?

like image 637
M. Ozn Avatar asked Jan 13 '16 07:01

M. Ozn


People also ask

What is parent div in HTML?

If we have a <p> inside a <div> element, the <div> is the parent of the <p> and the <p> is the child of the <div> <div> <p></p> </div>

What is Elem innerHTML?

The Element property innerHTML gets or sets the HTML or XML markup contained within the element. To insert the HTML into the document rather than replace the contents of an element, use the method insertAdjacentHTML() .

Is innerHTML safe?

'innerHTML' Presents a Security Risk The use of innerHTML creates a potential security risk for your website. Malicious users can use cross-site scripting (XSS) to add malicious client-side scripts that steal private user information stored in session cookies. You can read the MDN documentation on innerHTML .


2 Answers

Try this:

document.getElementById('MyId').outerHTML

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

like image 159
StudioTime Avatar answered Sep 26 '22 13:09

StudioTime


enter image description here


.outerHTML for outer, .innerHTML for inner.

like image 21
Jimbo Jonny Avatar answered Sep 22 '22 13:09

Jimbo Jonny