Advertisement

02.08.2006 at 07:41PM PST, ID: 21729635
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

Dynamically removing table rows

Asked by natfuse in JavaScript

Tags: , , ,

I have the following JS code which adds a new row to an existing HTML table:

function addRows(cid)
{
var tbody = document.getElementById(cid).getElementsByTagName("TBODY")[0];

var row = document.createElement("TR");

row.setAttribute('id', 'rowNum' + rowCount);

var t1 = document.createElement("TD");
t1.innerHTML='To:';

var t2 = document.createElement("TD");
t2.innerHTML='<input name="birthdayEmails" type="text" class="inputText" size="30" value="">';

var t3 = document.createElement("TD");

row.appendChild(t1);
row.appendChild(t2);
row.appendChild(t3);

tbody.appendChild(row)

t3.innerHTML='<a href=\'javascript:deleteRow("bdayTable", this)\'>Remove</a>';

rowCount++;

}

And this is the HTML code:

<table width="46%" id="bdayTable" border="1" cellpadding="3" cellspacing="0" class="bordered_table">
     <TBODY>
      <tr>
        <td>&nbsp;</td>
        <td><span class="style1">Email address</span></td>
      </tr>
     
        <tr>
          <td width="8%" class="style1">To:</td>
          <td width="20%">
            <input name="birthdayEmails" type="text" class="inputText" size="30" value="">
           
          </td>
          <td width="72%">&nbsp;
          </td>
        </tr>
     
        <tr>
          <td width="8%" class="style1">To:</td>
          <td width="20%">
            <input name="birthdayEmails" type="text" class="inputText" size="30" value="">
           
          </td>
          <td width="72%">&nbsp;
          </td>
        </tr>
     
        <tr>
          <td width="8%" class="style1">To:</td>
          <td width="20%">
            <input name="birthdayEmails" type="text" class="inputText" size="30" value="">
           
          </td>
          <td width="72%">&nbsp;
          </td>
        </tr>
     
        <tr>
          <td width="8%" class="style1">To:</td>
          <td width="20%">
            <input name="birthdayEmails" type="text" class="inputText" size="30" value="">
           
          </td>
          <td width="72%">&nbsp;
          </td>
        </tr>
     
        <tr>
          <td width="8%" class="style1">To:</td>
          <td width="20%">
            <input name="birthdayEmails" type="text" class="inputText" size="30" value="">
           
          </td>
          <td width="72%">&nbsp;
          </td>
        </tr>
     
     </TBODY>
    </table><BR>
    <a href="javascript:addRows('bdayTable')">Add row</a>

And the following code is being used to delete a row from the table:

function deleteRow(tblName, link)
{
  var tbl = document.getElementById(tblName);
  var tableRow = link.parentElement.parentElement; //gets TR object

  tbl.deleteRow(tableRow.rowIndex);
}

The addRows function works fine, the problem I am having is with the deleteRow function. I can't seem to get Javascript to correctly reference the table row object. Specifically, it seems that the line:

var tableRow = this.parentElement.parentElement;  //gets TR object

I've also tried:

var tableRow = this.parentNode.parentNode;

but either of these lines simply produce a value of 'undefined'.  What am I doing wrong here? How can I use the parentElement/parentNode parameter to reference the Table Row object that houses the 'deleteRow()' hyperlink?  If I can reference the TR object, then removing the row from the table will be a cinch! Please help!

Thanks

Start Free Trial
 
Keywords: Dynamically removing table rows
 
Loading Advertisement...
 
[+][-]02.08.2006 at 08:52PM PST, ID: 15909940

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.08.2006 at 09:27PM PST, ID: 15910071

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: JavaScript
Tags: javascript, table, remove, row
Sign Up Now!
Solution Provided By: gksinghiet
Participating Experts: 3
Solution Grade: A
 
 
[+][-]02.09.2006 at 01:15AM PST, ID: 15910848

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32