solai
solai

Reputation: 1

How to get value inside td using Dom

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

Upvotes: 0

Views: 9482

Answers (3)

Vithozor
Vithozor

Reputation: 620

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

Upvotes: 0

Wolfgang Kuehn
Wolfgang Kuehn

Reputation: 12926

Well, just navigate there. For example

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

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

Upvotes: 0

Alex Reitbort
Alex Reitbort

Reputation: 13696

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.

Upvotes: 1

Related Questions