Document.querySelector returning null

Asked

Viewed 115 times

0

I’m trying to fill an HTML table with data from a Sqlserver BD.

My HTML table:

var overViewForm = '<h3>OVERVIEW</h3>' +
                       '<table id="modelTable" class="table table-striped table-bordered table-hover table-condensed">' +
                           '<thead>' +
                               '<tr>' +
                                  '<th>idInstallation</th>' +
                                  '<th>project_Code</th>' +
                                  '<th>runId</th>' +
                                  '<th>block</th>' +
                                  '<th>input</th>' +
                                  '<th>state</th>' +
                                  '<th>operation</th>' +
                                  '<th>allDescription</th>' +
                                  '<th>reducedDescription</th>' +
                                  '<th>tstId</th>' +
                               '</tr>' +
                           '</thead>' +
                           '<tbody></tbody>' +
                       '</table>';
    
function overViewPrefab() {
    return overViewForm;
}

Obs: This table is inside a Javascript file. When I create it in HTML the code presents no problems.

Javascript that fills the table:

tableBody = document.querySelector("#modelTable > tbody");
    
function loadData() {

    const request = new XMLHttpRequest();  
    request.open("get", "http://localhost:52691//api/AppLock/run/all");
    
    request.onload = () => {
        const json = JSON.parse(request.responseText);
        populateTable(json);

    };

    request.send();

}

function populateTable(json) {
    // Populate table
    json.forEach((row) => {
        const tr = document.createElement("tr");

        Object.keys(row).forEach((cell) => {
            const td = document.createElement("td");
            td.textContent = cell;
            tr.appendChild(td);
        });

        tableBody.appendChild(tr);
    });
}

Important to say that I am calling these scripts at the bottom of HTML, I am also able to recover the data from BD, the problem is time to fill the table. I’ve debugged the entire code, apparently the problem is in the first line of Javascript:

tableBody = Document.querySelector("#modelTable > tbody");

that always returns null.

I am not able to identify why this mistake is happening or how to solve it. I wish someone could explain to me why this is happening, and give me a tip on how to solve this problem.

  • Testing here did not present any error and the table was populated normally. There must be something else that you did not mention and that must be the cause of the error.

  • @Some information that might help. The table is inside a Javascript file. When I put the table in HTML it normally fills.

  • What do you mean "inside a Javascript file"? I don’t understand.

  • @Sam I am using these Javascript files as templates, so I am changing the information of my div as I call these templates, using innerHTML.

  • I suggest you edit the question and put the code as is. You are talking about information that is not in the question and that greatly influences the result.

  • @Sam questions edited

  • You have to call tableBody = document.querySelector("#modelTable > tbody"); after you insert the table into HTML

  • @Sam ai this the problem, in index.html I am calling the table script before the script that loads the table. This should no longer be enough ?

  • It depends. In doing tableBody = document.querySelector("#modelTable > tbody");, the table must be in HTML, but returns null even.

  • @Sam some hint on how to do this ?

  • I was able to solve the problem. I was declaring tableBody = Document.querySelector("#modelTable > tbody"); outside a function, all I did was move inside the function that filled the table.

  • Probably the table isn’t ready yet, so it can’t identify the ID, you can try using: document.addEventListener("DOMContentLoaded", function(event) {&#xA; //verifica se o documento está pronto&#xA; });

Show 7 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.