I need to get all children of a DIV and their children too. I mean I need to get the HTML text of a DIV but I don't like to use innerHTML since it does not support the values of inputs in many browsers. How can I do that? :-/

I need to get all children of a DIV and their children too. I mean I need to get the HTML text of a DIV but I don't like to use innerHTML since it does not support the values of inputs in many browsers. How can I do that? :-/

<script language="JavaScript">
function test(){
var listOfChildNodes = document.getElementById('IDofTheDiv').childNodes;
var numberOfChildNodes = listOfChildNodes.length;
var str="";
for(i=0;i<numberOfChildNodes;i++){
   str += listOfChildNodes[i].tagName+"\n";// this is the way you access childNodes ... tagName is just for example purpose.
}
alert(str);
}
</script>
<div id= "IDofTheDiv">
<span></span>
<p></p>
<a></a>
</div>
<a href="javascript:test()">Click Here</a>

This is NOT what you exactly want .... but hope this would be of your help.

Regards ...

Thanks:)
And how should I do the reverse action? I mean if I have a string which contains HTML tags, how can I add it to a DIV content element by element and in the same precedence?
:-/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.