开发者

Javascript inserting data into HTML table

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:

Javascript inserting data into HTML table


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜