create table using javascript
Hi Folks,
I have the function below which creates a basic 3 row, 2 cell table. Could anyone tell me how I can create the table below, using my function?
Thankyou
HTML Code:
<table width="75%" border="1" >
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td rowspan="2" > </td>
</tr>
<tr>
<td> </td>
</tr>
</table>
Code:
function start() {
// get the reference for the body
var body = document.getElementsByTagName("body")[0];
// creates a <table> element and a <tbody> element
var tbl = document.createElement("table");
var tblBody = document.createElement("tbody");
// creating all cells
for (var j = 0; j <= 2; j++) {
// creates a table row
var row = document.createElement("tr");
for (var i = 0; i < 2; i++) {
// Create a <td> element and a text node, make the text
// node the contents of the <td>, and put the <td> at
// the 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);
}
// add the row to the end of the table body
tblBody.appendChild(row);
}
// put the <tbody> in the <table>
tbl.appendChild(tblBody);
// appends <table> into <body>
body.appendChild(tbl);
// sets the border attribute of tbl to 2;
tbl.setAttribute("border", "2");
}
Code:
function start(){
var root=document.getElementsByTagName('body')[0];
var oTab=document.createElement('table');
oTab.style.width='75%';
oTab.setAttribute('border','1');
var oTbo=document.createElement('tbody');
for(var i=0;i<3;i++){
var oTr=document.createElement('tr');
for(var j=0;j<2;j++){
if(i==2 && j==1){break}
else{
var oTd=document.createElement('td');
oTd.appendChild(document.createTextNode('\u0020'))
i==1&&j==1?oTd.setAttribute('rowSpan','2'):null;
oTr.appendChild(oTd)
}
}
oTbo.appendChild(oTr);
}
oTab.appendChild(oTbo);
root.appendChild(oTab)
}
Thanks for your reply.
Could you please tell me how this piece of code works?
Code:
oTd.appendChild(document.createTextNode('\u0020'))
i==1&&j==1?oTd.setAttribute('rowSpan','2'):null;
Code:
oTd.appendChild(document.createTextNode('\u0020'))
'\u0020' is the unicode escape notation for the empty space ( ). In javascript the escape unicode notation is safer in case of special characters.
Code:
i==1&&j==1?oTd.setAttribute('rowSpan','2'):null;
It is a ternary operator.
condition? if_true: if_false
The long equivalent is
if(i==1&&j==1){
oTd.setAttribute('rowSpan','2');
}
else{
null;//do nothing
}
Last edited by Kor; 03-21-2007 at 04:31 AM .
Thanks for you reply.
However, I cannot get this code to work, no text appars in the table cell.
Can you please help?
Thankyou
Code:
i==1&&j==1?oTd.setAttribute('rowSpan','3')&oTd.innerHTML == "text here":null;
I've managed to get this to work but I'm using to seperate statements, can I combine them somehow?
Code:
i==1&&j==1?oTd.setAttribute('rowSpan','3'):null;
i==1&&j==1?oTd.innerHTML = "<div id='itemdiv"+index+"'><input name='button' type='button' onclick=\"addElement("+index+",document.getElementById('itemdiv"+index+"').getElementsByTagName('select').length);\" value='Add' class='button'><input name='button' type='button' onclick=\"removeElement("+index+");\" value='Remove' class='button'><fieldset class='top'><div class='formleft'><h4 class='header'>item1</h4></div><div class='formright'><select name='item"+index+"_1' onChange=\"getProducts(this.options[this.selectedIndex].value,'subitem"+index+"_1');\" class='itempick'><cfoutput><cfloop from='1' to='#listlen(idlist)#'index='i'><option value='#ListGetAt(idlist,i)#'>#ListGetAt(titlelist,i)#</option></cfloop></cfoutput></select></div><div id='subitem"+index+"_1' class='items'></div></fieldset ></div>":null;
No, don't use innerHTML in that case, it is not a standard DOM method, you may get trouble
Code:
<script type="text/javascript">
function start(){
var root=document.getElementsByTagName('body')[0];
var oTab=document.createElement('table');
oTab.style.width='75%';
oTab.setAttribute('border','1');
var oTbo=document.createElement('tbody');
for(var i=0;i<3;i++){
var oTr=document.createElement('tr');
for(var j=0;j<2;j++){
if(i==2 && j==1){break}
else{
var oTd=document.createElement('td');
if(i==1&&j==1){
oTd.setAttribute('rowSpan','2');
oTd.appendChild(document.createTextNode('another text'))
}
else{
oTd.appendChild(document.createTextNode('some text'))
}
oTr.appendChild(oTd)
}
}
oTbo.appendChild(oTr);
}
oTab.appendChild(oTbo);
root.appendChild(oTab)
}
onload=start
</script>
If you need to create INPUT, use also createElement() method
I am somewhat new to the nuances of javascript and I wondered what these two lines of yours, Mr. KOR, are doing?
oTd.appendChild(document.createTextNode('\u0020'))
i==1&&j==1?oTd.setAttribute('rowSpan','2'):null;
especially the 2nd line. Please help me understand.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks