473,461 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dynamically remove TD using javascript.

Hi,

How to remove TD dynamically using javascript. I've mentioned below my code.

<table id="firstTbl" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr id="firstTr">
<td id="firstTd"> </td>
<td id="secondTd"> </td>
</tr>
</table>

I need to remove "firstTd" from the table..

Can help me anybody.. ?
Jul 13 '07 #1
2 16634
xNephilimx
213 Expert 100+
Hi,

How to remove TD dynamically using javascript. I've mentioned below my code.

<table id="firstTbl" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr id="firstTr">
<td id="firstTd"> </td>
<td id="secondTd"> </td>
</tr>
</table>

I need to remove "firstTd" from the table..

Can help me anybody.. ?
This question doesn't belong here, it belongs to he javascript forum. Hopefully it'll be moved. Anyway, to achieve what you want you should use some really simple DOM scripting. This is one way to do it, but is far from being the only way.

First you should decide what will trigger the td elimination, in this case I used a span that will trigger it, so that span has an onclick event that will trigger the function that must be passed with two parameters, the parent element and the child element, in this case the parent element would be the firstTr tr and the child elements would be the firstTd and the secondTd tds.

The JS function:
Expand|Select|Wrap|Line Numbers
  1. function remove(parent,child){
  2.     var p = document.getElementById(parent);
  3.     var c = document.getElementById(child);
  4.     p.removeChild(c);
  5. }
  6.  
The HTML code:
[HTML]
<table id="firstTbl" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr id="firstTr">
<td id="firstTd"><span onclick="remove('firstTr','firstTd');">Remove me</span></td>
<td id="secondTd"><span onclick="remove('firstTr','secondTd');">Remove me too</span></td>
</tr>
</table>
[/HTML]

As you can see, it's very very simple, I don't know the complexity of what you're trying to do, may be, if you're trying something complex you'll have to investigate a little more on DOM scripting, but I'm sure this simple script will serve almost all your purposes.

Best regards
Jul 13 '07 #2
Expand|Select|Wrap|Line Numbers
  1. <table id="anyId">
  2.     <thead>
  3.         <tr>
  4.             <td>HEAD 1</td>
  5.             <td>HEAD 2</td>
  6.             <td>HEAD 3</td>
  7.             <td>HEAD 4</td>
  8.             <td>HEAD 5</td>
  9.         </tr>
  10.     </thead>
  11.     <tbody>
  12.         <tr>
  13.             <td>BODY A 1</td>
  14.             <td>BODY A 2</td>
  15.             <td>BODY A 3</td>
  16.             <td>BODY A 4</td>
  17.             <td>BODY A 5</td>
  18.         </tr>
  19.         <tr>
  20.             <td>BODY B 1</td>
  21.             <td>BODY B 2</td>
  22.             <td>BODY B 3</td>
  23.             <td>BODY B 4</td>
  24.             <td>BODY B 5</td>
  25.         </tr>
  26.         <tr>
  27.             <td>BODY C 1</td>
  28.             <td>BODY C 2</td>
  29.             <td>BODY C 3</td>
  30.             <td>BODY C 4</td>
  31.             <td>BODY C 5</td>
  32.         </tr>
  33.     </tbody>    
  34. </table>
Expand|Select|Wrap|Line Numbers
  1. function deleteOnClick(table) {
  2.     this.table = document.getElementById(table)
  3.     this.rows = this.table.getElementsByTagName("tr")
  4.  
  5.     for (x=0; x<this.rows.length; x++) {
  6.         this.rows[x]._tr = this
  7.         this.rows[x].onclick = function() {
  8.         this._tr.remover(this)
  9.         }
  10.     }
  11. }
  12.  
  13. deleteOnClick.prototype.remover = function(obj) {
  14.     var confirmation = confirm("This will delete the row: " + "\n" + obj.innerHTML + "\n" + "Do you want to proceed?")
  15.         confirmation ? obj.parentNode.removeChild(obj) : null
  16. }
  17.  
Expand|Select|Wrap|Line Numbers
  1. new deleteOnClick('anyId')
  2.  
You can try this way so you dont have to pass an ID to each tr and call events directly from the tags
Jul 13 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: daniel kaplan | last post by:
Hi All, Been learning Javascript (via google) to create forms with pre-set values. As seen below. My problem I have found is this: I can't seem to figure out (or find so far via google) how to...
1
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created...
0
by: rwoo_98 | last post by:
I am using a repeater, with 3 columns of textboxes with X number of rows. The first two columns are enabled and allows the user to enter in numeric data. The third column contains the sum of the...
5
by: Dennis Fazekas | last post by:
Greetings, I am creating a web form which will all the user to add an unlimited number of email addresses. Basically I have 3 buttons, "Add Another Email", "-" to remove, and a "Save" button....
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
1
by: Steve2007 | last post by:
Hi I have the following form in my html page: <form name="mapserv" method=GET action=""> <input type="hidden" name="timeFiltering"> <input type="hidden" name="filteringType"> <input...
1
by: Sirisha | last post by:
Hi, I want javascript for displaying multiple textboxes dynamically. ADD-this is button. if i click button first time it will display 1 textbox and if i click 2nd time it will display another...
3
by: itsSase | last post by:
Is there any way to change a Div ID - dynamically using Javascript...?????
1
by: vivek kapile | last post by:
Title:Dynamically adding table row with a checkbox using JavaScript Author:Vivek Kapile Email:snipped Language:JavaScript Platform:JavaScript in ASP.net Technology:Used in ASP.net...
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.