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

html - How to write into a table with Javascript? - Stack Overflow

programmeradmin0浏览0评论
<Script>
function getExperience()
{
    var xp = document.getElementById('txt_XP').value;
    document.getElementByID("plank").innerHTML = xp/30;
}
</Script>

So here is my code, and my problem is that I seem to be unable to write over data in a table with the id's planl, oakPlank, teakPlank, and mahoganyPlank. I am thinking that I may be making an obvious mistake to someone who has done this sort of thing before, but I can't seem to catch it. Any help is much appreciated, and here is a snippet of my table, if it helps:

<tr>
    <td>Plank</td>
    <td id="plankXP">30</td>
    <td id="plank">0</td>
</tr>

EDIT: I didn't realize that this may be pertinent, my bad. This is the form I used to get input, which after putting an alert in to see if it could retrieve the XP, it functioned correctly:

<form name="experience" id="experience_frm" action="#">
    Experience: <input type="text" name="XP" id="txt_XP"/>
    <input type="submit" value="Go" onclick="getExperience();"/>
</form>
<Script>
function getExperience()
{
    var xp = document.getElementById('txt_XP').value;
    document.getElementByID("plank").innerHTML = xp/30;
}
</Script>

So here is my code, and my problem is that I seem to be unable to write over data in a table with the id's planl, oakPlank, teakPlank, and mahoganyPlank. I am thinking that I may be making an obvious mistake to someone who has done this sort of thing before, but I can't seem to catch it. Any help is much appreciated, and here is a snippet of my table, if it helps:

<tr>
    <td>Plank</td>
    <td id="plankXP">30</td>
    <td id="plank">0</td>
</tr>

EDIT: I didn't realize that this may be pertinent, my bad. This is the form I used to get input, which after putting an alert in to see if it could retrieve the XP, it functioned correctly:

<form name="experience" id="experience_frm" action="#">
    Experience: <input type="text" name="XP" id="txt_XP"/>
    <input type="submit" value="Go" onclick="getExperience();"/>
</form>
Share Improve this question edited Aug 10, 2017 at 13:30 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 9, 2013 at 4:30 shermanzachshermanzach 5811 gold badge6 silver badges15 bronze badges 8
  • Did you mean plankXP where you wrote txt_XP? – Matt Ball Commented Apr 9, 2013 at 4:32
  • 1 try putting alert in getExperience(), is it firing properly? – IT ppl Commented Apr 9, 2013 at 4:33
  • @MattBall I wouldn't think so. They seem to understand the difference between .value and .innerHTML, and even have the id including "txt"...so I'm guessing it's a textbox we haven't been shown. – Ian Commented Apr 9, 2013 at 4:40
  • I've made sure that getExperience is firing properly, the only problem is it won't seem to write over the 0 that I left as a placeholder in the third cell of that row. @ITppl – shermanzach Commented Apr 9, 2013 at 4:40
  • Are these rows (and ids) duplicated? – Ian Commented Apr 9, 2013 at 4:40
 |  Show 3 more ments

2 Answers 2

Reset to default 5

You have used the wrong document method. Javascript is case sensitive. You used:

document.getElementByID

for getting the id="plank" element. But you need to use:

document.getElementById

Notice the d (last character) change.

With this change, a simple example works for me:

http://jsfiddle/wqZAq/

Try This

<Script>
function getExperience()
{
    var xp = document.getElementById('txt_XP').value;
    var newxp = parseInt(xp);
    var div = newxp/30;
    document.getElementById("plank").innerHTML = div;
}
</Script>
<table>
<tr>
    <td>Plank</td>
    <td id="plankXP">30</td>
    <td id="plank">0</td>
</tr>
</table>
<input type="text" id="txt_XP" name="txt_XP" />
<input type="button" onclick="getExperience();" />
发布评论

评论列表(0)

  1. 暂无评论