I am trying to make the table that I am creating on the fly with JavaScript into a .dataTable. I have compared my code to my coworkers and it is almost identical. I have made the necessary changes, but I keep getting the JavaScript error: "Cannot read property 'parentNode' of null" in the console.
All of the code is executing perfectly. The new table is being displayed in the browser, but once it tries to run the line $('#data').dataTable({"paging": false}); it gives me the error.
Can anyone see what is causing this? Maybe a scope error, or a parentheses/bracket in wrong spot.
function Load(p1, p2) {
var work= "";
$('#div1').empty();
var tablearea = document.getElementById('div1'),
table = document.createElement('table');
table.id = "data";
table.style.fontSize = "small";
table.width = "100%";
var tbody = document.createElement('tbody');
tbody.id = "dataTB";
table.appendChild(tbody);
tablearea.appendChild(table);
var url = sessionStorage.baseUrl + "XXXXXXXXX";
$.getJSON(url)
.done(function (data) {
var results = $.parseJSON(data);
if (results != null)
{
for (i = 0; i < results.length; i++) {
work += "<tr><td>" + results.data[i].info + "</td></tr>";
}
}
$('#dataTB').html(work);
$('#data').dataTable({"paging": false});
})
.fail(function (jqXHR, textStatus, err) {
alert('Error: ' + err);
});
}
table
defined? Where is the error occuring, according to the console (because I don't see any reference toparentNode
in your code)? – talemyn May 15 at 17:22parentNode
isn't a variable, it's a property of the JSNode
object ( developer.mozilla.org/en-US/docs/Web/API/Node/parentNode ) that represents the parent of a current element node. Based on the error, somewhere in the logic fordataTable
it is trying to find theparentNode
of a non-existant element. My bet is that it, you are calling$('#data').dataTable({"paging": false});
before thedata
table element has finished being added to the DOM, so it can't find it. – talemyn May 15 at 17:48