2

I am trying to get a table to display properly using the following code. The problem is that the </tbody> and </table> tags are being applied before the for loop enters the data into the table. The table head displays fine and the data displays fine as well, just not in the table. What am I doing wrong? I tried moving the last line into the loop with in if statement but the same happens.

    $.getJSON("adriver.php?type=get", function(data, status){
    document.getElementById("aRightContent").innerHTML =
            "<table class='table table-hover'>" +
            "<thead>" +
            "<tr>" +
                "<th><i class='fa fa-check-square'></iclass></th>" +
                "<th>ID</th>" +
                "<th>Firstname</th>" +
                "<th>Lastname</th>" +
                "<th>UserName</th>" +
            "</tr>" +
            "</thead>" +
            "<tbody>";

    for(var i = 0; i < data.length; i++){
        document.getElementById("aRightContent").innerHTML +=
                "<tr>" +
                    "<td><input type='radio' name='selectedDriver' value='" + data[i].dID + "'></td>" +
                    "<td>" + data[i].dID + "</td>" +
                    "<td>" + data[i].dFirstName + "</td>" +
                    "<td>" + data[i].dLastName + "</td>" +
                    "<td>" + data[i].dUsername + "</td>" +
                "</tr>";
    }
    document.getElementById("aRightContent").innerHTML += "</tbody></table>";
});

Thank you for any help.

1
  • 2
    can't write to the dom as if it is a text editor. Commented Oct 11, 2015 at 15:04

2 Answers 2

2

You cannot add partial <table> and </table> using the innerHTML, as the browser tends to fill it out for you. Best way I would suggest is to build the whole HTML and then append.

So you should have something like:

finalHTML = "<table class='table table-hover'>" +
        "<thead>" +
        "<tr>" +
            "<th><i class='fa fa-check-square'></iclass></th>" +
            "<th>ID</th>" +
            "<th>Firstname</th>" +
            "<th>Lastname</th>" +
            "<th>UserName</th>" +
        "</tr>"
        "</thead>" +
        "<tbody>";
finalHTML += "..."  // Other code.

Also, since you are using jQuery, use the same components. Instead of document.getElementById("aRightContent").innerHTML, use:

$("#aRightContent").html()
Sign up to request clarification or add additional context in comments.

1 Comment

Works great! Thanks!
2

Just write the whole thing in a string:

var htmlString = "<table class='table table-hover'>" +
        "<thead>" +
        "<tr>" +
            "<th><i class='fa fa-check-square'></iclass></th>" +
            "<th>ID</th>" +
            "<th>Firstname</th>" +
            "<th>Lastname</th>" +
            "<th>UserName</th>" +
        "</tr>"
        "</thead>" +
        "<tbody>";

etc...

And use then asign to innerHTML = htmlString at the end.

Reason

You shouldn't assign to document.getElementById("aRightContent").innerHTML an incomplete piece of HTML code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.