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

Javascript inserting data into HTML table - Stack Overflow

programmeradmin4浏览0评论

This is a rather simple question, I am having problems inserting data from Javascript into an HTML table.

Here is an excerpt of my JavaScript:

UPDATED - I got rid of the two loops and simplified it into one, however there is still a problem..

    for (index = 0; index < enteredStrings.length; index++) {
        output.innerHTML += "<td>" + enteredStrings[index] + "</td>"
        + "<td>" + enteredStringsTwo[index] + "</td>";
        nameCounter++;
        total.innerHTML = "Total: " + nameCounter;
    }

And here is an except of my HTML page:

<table id="nameTable">
   <tr>
     <th>First</th><th>Last</th>
   </tr>

Updated Picture:

This is a rather simple question, I am having problems inserting data from Javascript into an HTML table.

Here is an excerpt of my JavaScript:

UPDATED - I got rid of the two loops and simplified it into one, however there is still a problem..

    for (index = 0; index < enteredStrings.length; index++) {
        output.innerHTML += "<td>" + enteredStrings[index] + "</td>"
        + "<td>" + enteredStringsTwo[index] + "</td>";
        nameCounter++;
        total.innerHTML = "Total: " + nameCounter;
    }

And here is an except of my HTML page:

<table id="nameTable">
   <tr>
     <th>First</th><th>Last</th>
   </tr>

Updated Picture:

Share Improve this question edited Dec 16, 2010 at 6:02 TheStandardRGB asked Dec 16, 2010 at 5:06 TheStandardRGBTheStandardRGB 2092 gold badges3 silver badges8 bronze badges 5
  • Why do you have 2 loops? What's the difference between them? – David Tang Commented Dec 16, 2010 at 5:47
  • Fixed but there's still a problem. – TheStandardRGB Commented Dec 16, 2010 at 6:04
  • check edit2 in my answer and see if that works for you. – Mahesh Velaga Commented Dec 16, 2010 at 6:12
  • Sent you an email at your Gmail address. I really appreciate your help. – TheStandardRGB Commented Dec 16, 2010 at 6:24
  • Updated my answer with the code that works (after seeing the actual code that is sent through email) – Mahesh Velaga Commented Dec 16, 2010 at 7:08
Add a ment  | 

4 Answers 4

Reset to default 6

Try this (edited):

var tableContent = '<tr>';
for (index = 0; index < enteredStrings.length; index++) {
    tableContent += "<td>" + enteredStrings[index] + "</td>";
    nameCounter++;  // I don't know if this should be there, 
                    // logically the counter should be incremented here as well?
    total.innerHTML = "Total: " + nameCounter;
}
tableContent += '</tr><tr>';

for (index = 0; index < enteredStringsTwo.length; index++) {
    tableContent += "<td>" + enteredStringsTwo[index] + "</td>";
    nameCounter++;
    total.innerHTML = "Total: " + nameCounter;
}
tableContent += '</tr>';
output.innerHTML += tableContent;

Edit2 (for updated question code):

var tableContent = '<tr>';
for (index = 0; index < enteredStrings.length; index++) {
    tableContent += "<td>" + enteredStrings[index] + "</td>"
    + "<td>" + enteredStringsTwo[index] + "</td>";
    nameCounter++;
    total.innerHTML = "Total: " + nameCounter;
}
tableContent += '</tr>';
output.innerHTML += tableContent;

Edit3 (after looking at the code sent in email):

var tableContent = "";

for (index = 0; index < enteredStrings.length; index++) {
    tableContent += "<tr><td>" + enteredStrings[index] + "</td>"

    + "<td>" + enteredStringsTwo[index] + "</td></tr>";

    nameCounter++;
    total.innerHTML = "Total: " + nameCounter;
}
output.innerHTML = tableContent;

instead of closing the td you are opening new ones try

    for (index = 0; index < enteredStrings.length; index++) {
    output.innerHTML += "<td>" + enteredStrings[index] + "</td>";
    total.innerHTML = "Total: " + nameCounter;
}

for (index = 0; index < enteredStringsTwo.length; index++) {
    output.innerHTML += "<td>" + enteredStringsTwo[index] + "</td>";
    nameCounter++;
    total.innerHTML = "Total: " + nameCounter;
}

UPDATE: you are appending the html to the table instead of the row. in this case, the browser created a row for you automatically after the each td is appended.

With slight modifications in your code,

    var outputTbl = document.getElementById('nameTable');

    var output = document.createElement("tr");
    outputTbl.appendChild(output);

    for (index = 0; index < enteredStrings.length; index++) {
        output.innerHTML += "<td>" + enteredStrings[index] + "</td>";
        total.innerHTML = "Total: " + nameCounter;
    }

    for (index = 0; index < enteredStringsTwo.length; index++) {
       output.innerHTML += "<td>" + enteredStringsTwo[index] + "</td>";
       nameCounter++;
       total.innerHTML = "Total: " + nameCounter;
    }

If you need to add inner html code here.

<table id="nameTable" style="width:300px;">
    <tr>
        <th>First</th><th>Last</th>
    </tr>
</table>

You can use Jnerator in this case.

If this is your data:

var data = [
    { first: 'Cole', last: 'Alan'},
    { first: 'Michael', last: 'Scott'}
]

You can add them to you table in next way:

for(var i=0; i<data.length; i++) {
    var item = data[i];
    var row = $j.tr({ child:[$j.td(item.first), $j.td(item.last)] });
    nameTable.appendChild(row.dom());
}
nameTotal.innerHTML = 'Total: ' + data.length;

This is example.

发布评论

评论列表(0)

  1. 暂无评论