1

I'm trying to add a table into an HTML page,using a js function.
I saw many examples for adding\removing data from an existing table,but can't find example of adding a new table that hadn't existed before.
I am having inside my javascript variables with all the information I need for the table,now I only need to find a way to do that...lets assume my var's are v1,v2,v3,v4 and i want to creat the following table:

v1  v2
v3  v4

what is the correct code that would execute the above?

2
  • 2
    wrapper.innerHTML = '<table><tr><td>' + v1 + '</td><td>' + v2 + '</td></tr><tr><td>' + v3 + '</td><td>' + v4 + '</td></tr></table>'; where wrapper is a reference to some DOM-element in which you want to put the table. If the number of rows/cells is not fixed, I recommend a function which creates the HTML string dynamically. Commented Jul 30, 2011 at 17:26
  • 1
    Post the examples you found and maybe we'll be able to help you modify them. Commented Jul 30, 2011 at 17:26

6 Answers 6

4
var v1 = 'v1', 
    v2 = 'v2', 
    v3 = 'v3', 
    v4 = 'v4';

var table = document.createElement('table');

var row = table.insertRow(0);
row.insertCell(0).innerHTML = v1;
row.insertCell(1).innerHTML = v2;

row = table.insertRow(1);
row.insertCell(0).innerHTML = v3;
row.insertCell(1).innerHTML = v4;

document.body.appendChild( table );

Example: http://jsfiddle.net/XPpNF/1/

Sign up to request clarification or add additional context in comments.

Comments

1

Start with var tableElem = document.createElement('table')

Comments

1

Lot's of answers here should work already.

BUT! Some versions of Internet Explorer can't alter the content of a <table> element once it's been created. So you can't always use tableElement.appendChild(trElement) for instance.

But you can use innerHTML to replace the html content of a table instead - that works.

2 Comments

I think your statement about IE is too broad. You can certainly alter the content of a table in IE. There are just some issues with respect to where you can use innerHTML. Here's an interesting article from a guy who was involved in the implementation of IE's innerHTML. "At first, I thought that IE was not capable of performing the redraw for modified tables with innerHTML, but then I remembered that I was responsible for this limitation!"
@patrick dw: It was indeed broad and imprecise, and yes there are other ways around it. I just wanted to note that tables are a special case when it comes to DOM manipulation. Thanks for the link - interesting reading!
0

just create the html code of your table and add it to an existing div for example:

var html = "<table";
html += "<tr><td>v1</td><td>v2</td></tr>";
html += "<tr><td>v3</td><td>v4</td></tr>";
html += "</table>";
document.getElementById('idOfYourDiv').innerHTML = html;

You can also build the table using document.createElement(table) and creating all the elements inside the table, but the first way using the string is faster.

Hope this helps

2 Comments

document.createElement is much faster than having to parse text into elements.
i suspect that interacting with the dom and creating all the elemnts one by one it's not faster for the CPU, anyway i mean that it's faster for you to type in this way
0

You could use jQuery to append the code you created (even if not mandatory for what you need):

var table += <table>
table += <tr> ... loop over your data variables to fill data ...
table += </tr>
table += </table>
§("# your wished table-id").html(data);

Suggestion given by Patrik is surely more elegant. This one lets you create each single tag, so that maybe zou ca directly insert extra style attributes as needed.

1 Comment

This isn't even close to valid javascript
0

Assuming you're using jQuery, that'd be something like

var awesomeTable = $("<table/>");
$("#parentElement").append(awesomeTable);

var awesomeTr = $("<tr/>");
awesomeTable.append(awesomeTr);

awesomeTr
    .append($("<td/>", {html: v1}))
    .append($("<td/>", {html: v2}))
    .append($("<td/>", {html: v3}))
    .append($("<td/>", {html: v4}));

3 Comments

Load jQuery just to create a table? Overkill?
I was going for a one-rowed table, because it was formatted like one in the original version of your post, but it's fairly easy to modify my answer for any number of rows; it'll just be more verbose.
@patrick no, of course not :) I just meant that jQuery is so widely used these days that it's a 95% chance that the OP is either using it or it will cover many bases in any project anyway.

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.