4
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required. Create table using Javascript I have a JavaScript function which creates a table with 3 rows 2 cells. Could anybody tell me how I can create the table below using my function (i need to do this for my situation)? My HTML: <table width="100%" border="1"> <tr> <td>&nbsp;</td> <td>&nbsp;</td> </tr> <tr> <td>&nbsp;</td> <td rowspan="2">&nbsp;</td> </tr> <tr> <td>&nbsp;</td> </tr> </table> JavaScript: function tableCreate() { //body reference var body = document.getElementsByTagName("body")[0]; // create elements <table> and a <tbody> var tbl = document.createElement("table"); var tblBody = document.createElement("tbody"); // cells creation for (var j = 0; j <= 2; j++) { // table row creation var row = document.createElement("tr"); for (var i = 0; i < 2; i++) { // create element <td> and text node //Make text node the contents of <td> element // put <td> at end of the table row var cell = document.createElement("td"); var cellText = document.createTextNode("cell is row "+j+", column "+i); cell.appendChild(cellText); row.appendChild(cell); } //row added to end of table body tblBody.appendChild(row); } // append the <tbody> inside the <table> tbl.appendChild(tblBody); // put <table> in the <body> body.appendChild(tbl); // tbl border attribute to tbl.setAttribute("border", "2"); }

HTML - Create Table Using Javascript - Stack Overflow

Embed Size (px)

DESCRIPTION

The document is download via internet

Citation preview

  • Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, noregistration required.

    Create table using Javascript

    I have a JavaScript function which creates a table with 3 rows 2 cells.Could anybody tell me how I can create the table below using my function (i need to do this for mysituation)?My HTML:

    JavaScript:

    function tableCreate() {

    //body reference

    var body = document.getElementsByTagName("body")[0];

    // create elements and a

    var tbl = document.createElement("table");

    var tblBody = document.createElement("tbody");

    // cells creation

    for (var j = 0; j

  • javascript html table

    asked Feb 1 '13 at 9:54Manny Fitzgerald102 1 1 5

    4 Answers

    This should work (from a few alterations to your code above).

    function tableCreate(){

    var body=document.getElementsByTagName('body')[0];

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

    tbl.style.width='100%';

    tbl.setAttribute('border','1');

    var tbdy=document.createElement('tbody');

    for(var i=0;i

  • }

    body.appendChild(tbl);

    }

    tableCreate();

    Also, this doesn't use some "bad practices", such as setting a attribute instead ofusing CSS, and it accesses the through instead of

    ;

    border

    body document.body

    document.getElementsByTagName('body')[0]

    edited Dec 4 '14 at 10:10 answered Feb 1 '13 at 10:38Cerbrus22.7k 4 26 55

    Insert title here

    var student;

    for(var j=0;j

  • create table

    function create() {

    var row = parseInt(document.getElementById("row").value);

    var col = parseInt(document.getElementById("col").value);

    var tablestart="";

    var tableend = "";

    var trstart = "";

    var trend = "";

    var tdstart = "";

    var tdend = "";

    var data="data in cell";

    var str1=tablestart + trstart + tdstart + data + tdend + trend + tableend;

    document.write(tablestart);

    for (var r=0;r