Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display innerHTML of child

Tags:

javascript

i have:

<td id="td1">
     <div>
          aaaaaa
     </div>
</td>

how can i get the innerHTML of the child of "td1" ?

function displaymessage() {
    var i = 0;
    alert(document.getElementById("td1").childNodes[0].innerHTML);
}

dosen't work.

like image 689
hhh3112 Avatar asked Dec 01 '22 02:12

hhh3112


1 Answers

Some browsers interpret a line break as the first childNode. So you can do:

document.getElementById("td1").childNodes[1].innerHTML

or a safer method

document.getElementById("td1").getElementsByTagName('div')[0].innerHTML

[edit 2019] or more modern

document.querySelector("td1 > div").innerHTML
like image 157
KooiInc Avatar answered Dec 10 '22 13:12

KooiInc