最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript add data to table cell - Stack Overflow

programmeradmin3浏览0评论
<table border='1' id='output'>

<tr>

<td>

</td>

</tr>

</table>

my javascript code

 document.getElementById("output").childNodes[0].childNodes[0].nodeValue = ajaxRequest.responseText;

Doesnt work please help

<table border='1' id='output'>

<tr>

<td>

</td>

</tr>

</table>

my javascript code

 document.getElementById("output").childNodes[0].childNodes[0].nodeValue = ajaxRequest.responseText;

Doesnt work please help

Share Improve this question asked Apr 13, 2011 at 6:14 crowsocrowso 2,0779 gold badges33 silver badges38 bronze badges 1
  • you can assign an id to your td element and update it by find by id. – Naren Sisodiya Commented Apr 13, 2011 at 6:25
Add a ment  | 

5 Answers 5

Reset to default 2

Using JQuery u can do it easly as given below:

$(document).ready(function(){
 $("#output tr td").text("JQUERY HELP");
});

or if u want to continue with javascript u can refer other posted answers.

CLICK HERE TO SEE THE DEMO

document.getElementById("output").children[0].children[0].children[0].innerHTML;

You have two things wrong:

  1. That's not a valid <table>.

Tables have to have a <tbody> tag. Which is probably getting added by the browser, which means you need to go one level deeper to access the <td> element.

The second thing, nodeValue will always be null for a non-text node, which is what a <td> is. Instead use the innerHTML property to alter the element's text.

After correcting those two things, your code should look like this:

 document.getElementById("output").childNodes[0].childNodes[0].childNodes[0].innerHTML = ajaxRequest.responseText;
<table border='1' id='output'>
    <tr>
        <td></td>
    </tr>
</table>

$(function(){
   $('#output td').append("blaa");
});

Check this

Hope this helps.

Alex is right, there is a tbody TAg. try below one: document.getElementById("output").getElementsByTagName("td")[0].innerHTML="test1"

发布评论

评论列表(0)

  1. 暂无评论