473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Remove a table row

I need some help modifying this code. The code was orginally setup to
add/remove a single table row. I modified it to add 2 (two) rows with
one column that spans both rows. That part works. I just need help
with the removerow function. Thanks for any help you can offer me.
Here is the code:

<html>
<head>
<title>New Document</title>
<script type="text/javascript">
// The counter just keeps track of which row we've added so we can
// distinguish them. It is only used for generating text, none of
// the functions depend on this number for node manipulation.
var counter = 1;
var plain = true;

function addrow()
{
// Obtain a reference to the tbody node.
var tbody = document.getElementById("main").getElementsByTagNa me("tbody").item(0);

if(plain)
{
plain=false;
}
else
{
plain=true;
}
for(x=1;x<3;x++)
{

// Generate a new row.
var tr = document.createElement("tr");
if(plain)tr.bgColor = "#CCCCCC";
// Append the two cells to the element.
if(x==1)
{
td = document.createElement("td");
td.rowSpan = "2";
td.innerHTML = '<input type="button" value="Delete"
onclick="deleterow(this)" />';
tr.appendChild(td);
}
for(i=1;i<4;i++)
{
var td = document.createElement("td");
td.innerHTML = '<div align="right"><a onClick="removerow(this)"><img
src="images/delete2.jpg" width="16" height="16"></A></div>';
tr.appendChild(td);
}

// Now append the new row to the tbody.
tbody.appendChild(tr);
}
}

function deleterow(node)
{
// Obtain a reference to the containing tr. Use a while loop
// so the function can be called by passing any node contained by
// the tr node.
var tr = node.parentNode;
while (tr.tagName.toLowerCase() != "tr")
tr = tr.parentNode;

// Remove the tr node and all children.
tr.parentNode.removeChild(tr);
}
</script>
</head>
<body>
<table id="main" border="2">
<tr>
<td>Dynamic Table Demo</td>
<td><input type="button" onclick="addrow()" value="Add Row"/></td>
</tr>
</table>
</body>
</html>
Jul 20 '05 #1
1 7476
Steve S wrote:
I modified it to add 2 (two) rows with
one column that spans both rows. That part works. I just need help
with the removerow function. if(plain)
{
plain=false;
}
else
{
plain=true;
}
What about
plain=!plain;
td.innerHTML = '<div align="right"><a onClick="removerow(this)"><img


removerow is undefined, while deleterow happily exists:-)

If you insert two rows then you must remove two as well, the problem is
to determine which rows to remove. There are many ways to find this, you
could (1) simply tell which rows to remove when calling the function or
(2) guess which one to remove, for instance checking the rowspan
property and making appropriate decision on whether to remove the next
sibling or the previous sibling as well.

However, I don't really understand the logic of your buttons/links
(deleterow/removerow may really be different functions); hopefully the
techniques below will nevertheless help you into building an approriate
script.
<script type="text/javascript">
var addRowTo = (function() {

var getBgColor=(function(){
var k=0, bg1="#ccc", bg2="transparent";
return (function() {
return k==0 ?
(k=1, bg1) :
(k=0, bg2);
});
})();

return (function (tableId){
var d, tbody, tr, td, bgc, ii, j;

d=document;
bgc=getBgColor();
tbody=d.getElementById(tableId).
getElementsByTagName("tbody").
item(0);

for(ii=0; ii<2; ii++) {
tr=d.createElement("tr");
tr.style.backgroundColor=bgc;

if(ii==0) {
td=d.createElement("td");
td.rowSpan="2";
td.innerHTML="<input type='button'"+
" value='Delete'"+
" onclick='removeRow(this)'>";
tr.appendChild(td);
}

for(j=0; j<3; j++) {
td=d.createElement("td");
td.innerHTML="<a onClick='removeRow(this)'>x<\/a>";
tr.appendChild(td);
}

tbody.appendChild(tr);
}
});
})();

function removeRow(el){
var cel;

while(el && el.nodeName.toLowerCase()!="tr")
el=el.parentNode;

if(el && el.parentNode){
cel=el.getElementsByTagName("td").item(0);
el.parentNode.removeChild(
el[(cel.rowSpan=="2"?"next":"previous")+"Sibling"]);
el.parentNode.removeChild(el);
}
}
</script>

<table id="main" border="2">
<tbody>
<tr>
<td>Dynamic Table Demo</td>
<td colspan="3">
<input type="button"
onclick="addRowTo('main')"
value="Add Row">
</td>
</tr>
</tbody>
</table>
HTH
Yep.
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: kie | last post by:
hello, i have a table that creates and deletes rows dynamically using createElement, appendChild, removeChild. when i have added the required amount of rows and input my data, i would like to...
1
by: Soul | last post by:
Hi, I have a DataGrid which works. But if I load a new table to the DataGrid, it will still show those old data I load earlier. private void loadTableA() { if ( openFileDialog.ShowDialog()...
3
by: axlq | last post by:
I created an experimental page at: http://sunbeam.rahul.net/~unicorn/csstest.html ....which is a standard header + nav bar + 2-column content + footer CSS layout. The left column is 10em wide...
4
by: G .Net | last post by:
Hi I have a DataSet with several DataTables. I have set up relations between these tables. I want to delete all the tables and re-fill them. However, when I try to do so, even after using...
7
by: Susan Mackay | last post by:
I have a data table that is connected to a database table with a data adapter in the 'standard' manner. However I want to be able to remove selected rows from the data table (i.e. no longer...
2
by: mrjoka | last post by:
hi experts, i'm developing a page in ASP but i'm doing also some javascript insode the page. i'm creating a frame and i want to loop this frame with a duplicateloop function so the form will be...
3
by: wizardRahl | last post by:
Hello, I'm doing a page for homework that incorporates a javascript form. I initially had to removed all of the HMTL style information and replace it with CSS. I feel that I've done most of this...
12
by: coolminded | last post by:
plz urgent help needed i have one data grid in the form. i have to export the data from the datagrid to the word. i just exported the data in MS word into a table. but i want to remove the table...
1
by: bumpy | last post by:
I wrote this pretty slick DHTML table that lets you add/remove rows and show/hide columns on the fly. It works perfectly in IE7, but it doesn't behave in Firefox 2.0.0.4. You can see it in action...
1
by: rakeshnair | last post by:
i wrote a code in jsp to create dynamic table..the problem is i need data base connection when cursor moves from one cell to other... eg...when i enter product id in the first cell, the product name...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.