<html> <head> </head> <body> <div id="content_Data"></div> <script type="text/javascript"> function auto() { alert("auto called"); document.getElementById('content_Data').innerHTML='<div><table><tr><td>10</td><td>20</td></tr></table></div>'; alert(document.getElementById('content_Data').innerHTML); getelements(); } function getelements(){ var searchElement=document.getElementById('content_Data').getElementsByTagName("div"); for( var i=0; i<searchElement.length; i++ ) { var child_length=searchElement[i].childNodes.length; for( j=0; j<child_length; j++ ) { alert(searchElement[i].childNodes[j].nodeValue); } } } </script> <script>auto();</script> </body> </html> Add a comment |
3 Answers
try looking at innerHTML of td node. Or if you want only text, then it is innerText for IE and textContent for others.
alert(searchElement[i].childNodes[j].innerHTML) also, jQuery will greatly simplify your code.
Comments
Well, just navigate there. For example
document.getElementById('content_Data').firstChild.firstChild.firstChild gets you the first table cell (with value 10).
1 Comment
user113716
Many browsers automatically insert a
<tbody> when it is missing, but this isn't a guarantee. As such .firstChild.firstChild... etc will be unreliable.