Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
86
rated 0 times [  88] [ 2]  / answers: 1 / hits: 9370  / 11 Years ago, sun, february 2, 2014, 12:00:00

I have a JS function that takes a 2D array and a div. It creates a table from the array and appends it to the div. The div currently displays: [object HTMLTableElement]. How can I display it as the table instead?



I call the function with: createTable(myArray, $(#tblDiv));.



Later:



function createTable(array, tableDiv)
{
var tbl = document.createElement('table');
tbl.style.border = 1px solid black;
for(var i = 0; i < array.length; i++)
{
var tr = tbl.insertRow();
for(var j = 0; j < array[i].length; j++)
{
var td = tr.insertCell();
}
}
tableDiv.append(<br> + tbl);
return;
}

More From » jquery

 Answers
3

<br> + tbl is going to evaluate to <br>[object HTMLTableElement]. This is string concatenation, if you try to add a string to a non string it will be converted to a string then added so an HTMLTableElement as a string is [object HTMLTableElement].

What you want to do is append the table by itself



tableDiv.append(<br>);
tableDiv.append(tbl);

[#48132] Saturday, February 1, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kelsy

Total Points: 486
Total Questions: 86
Total Answers: 76

Location: El Salvador
Member since Sun, Sep 12, 2021
3 Years ago
kelsy questions
Tue, Oct 19, 21, 00:00, 3 Years ago
Tue, Aug 4, 20, 00:00, 4 Years ago
Wed, Jun 17, 20, 00:00, 4 Years ago
Fri, Jan 10, 20, 00:00, 4 Years ago
;