Go Back   PCMech Forums > Help & Discussion > Internet, Web Applications, & The Cloud

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 03-05-2004, 10:35 AM   #1
Member (9 bit)
 
Join Date: Jan 2001
Location: Kalamazoo, MI
Posts: 314
Send a message via ICQ to sdkramer Send a message via AIM to sdkramer Send a message via Yahoo to sdkramer
add a row to a table in javascript

Is there any way I can add a row to a table dynamically with javascript.

I would like to have a button who's onlick event adds an item to a table

I tried generating this table in text and using innerHTML of a div tag and the tds don't work it's like it just puts all the text together. Is there anything I can do?

TIA,

Seth
sdkramer is offline   Reply With Quote
Old 03-05-2004, 11:28 AM   #2
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
check out this:
http://javascript.internet.com/misce...add-a-row.html
aym is offline   Reply With Quote
Old 03-05-2004, 12:40 PM   #3
Member (9 bit)
 
Join Date: Jan 2001
Location: Kalamazoo, MI
Posts: 314
Send a message via ICQ to sdkramer Send a message via AIM to sdkramer Send a message via Yahoo to sdkramer
I dont' mean to be ungrateful but is there a way to remove a row as well?
sdkramer is offline   Reply With Quote
Old 03-05-2004, 01:49 PM   #4
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
code for adding, removing and renaming a row, taken from this page:
http://www.experts-exchange.com/Web/..._20781550.html
PHP Code:
function renameRows(tBodyEl)
{  
  var 
rows tBodyEl.getElementsByTagName("tr");    
  var 
rowcellinputj;
 
  for(var 
0rows.lengthi++)
  {
    
1;
    
row rows[i];
    
cell row.getElementsByTagName("td")[0];
    
input cell.getElementsByTagName("input")[0];
       
    
input.name "count" j;    
    
input.value j;
  }
  
document.getElementById("textfieldcount").value rows.length;
}

function 
addRow()
{
  var 
table document.getElementsByTagName("table")[0];
  var 
tBody table.getElementsByTagName("tbody")[0];
  var 
rows tBody.getElementsByTagName("tr");    
 
  var 
newRow tBody.appendChild(rows[0].cloneNode(true));
 
  
renameRows(tBody);
}

function 
removeRow()
{
  var 
table document.getElementsByTagName("table")[0];
  var 
tBody table.getElementsByTagName("tbody")[0];
  var 
rows tBody.getElementsByTagName("tr");  
  var 
row rows[0];
 
  if(
rows.length 1)
  {
    
tBody.removeChild(row);
    
renameRows(tBody);
  }
  else
    
alert ("You cannot delete the last remaining row");

aym is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 06:39 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2