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

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Well, just navigate there. For example

document.getElementById('content_Data').firstChild.firstChild.firstChild 

gets you the first table cell (with value 10).

1 Comment

Many browsers automatically insert a <tbody> when it is missing, but this isn't a guarantee. As such .firstChild.firstChild... etc will be unreliable.
0

You can change the getElementsByTagName("div") to get the td elements:

var searchElement=document.getElementById('content_Data').getElementsByTagName("td"); 

And, it's better to declare the j variable on the second for :D. That's all you need to change

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.