0

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? :-/

2
Contributors
2
Replies
4
Views
8 Years
Discussion Span
Last Post by ehsun7b
0

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 ...

0

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?
:-/

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.