954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Get all children and their childeren - not innerHTML

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

ehsun7b
Newbie Poster
7 posts since Nov 2008
Reputation Points: 10
Solved Threads: 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 ...

sasankasekhar
Light Poster
31 posts since Jan 2007
Reputation Points: 10
Solved Threads: 4
 

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

ehsun7b
Newbie Poster
7 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You