Friday, February 12, 2016

How to Create table using javascript

How to create table in javascript ?

use this below to create html table in javascript
html table like below will add to your div element dynamically
<table border="1">
<tr>
<td>
Hello world.
<td/>
<tr/>
<table/>

Code :
var tr,td;   ---variables for row and cell
var Table =document.createElement('TABLE'); ---Here table will create
Table.setAttribute("border",1);    -- adds border to table.
tr=Table.insertRow(0);  --- new row for Table
td=tr.insertCell(0) ;        ---- new cell for tr (Row)
td.innerHTML ="Hello World";   --- Text that you want to display

'lets add this table to Document.

document.getElementById('divMain').appendChild(Table);  --- Div is your body div where you want to add.

more example
for(i =0;i< 20;i++)
{
 tr=Table.insertRow(Table.rows.length);
td=tr.insertCell(tr.cells.length);
td.innerHTML="Hello";
td=tr.insertCell(tr.cells.length);
td=tr.insertCell(tr.cells.length);
td.innerHTML ="World";
 }

document.getElementById("divMain").appendChild("Table");



---- Queries post here.













No comments:

Post a Comment