Solved

Deleting contents of <tbody> from within a seperate <tbody>

Posted on 2004-08-31
3
380 Views
Last Modified: 2008-03-17
Sorry if that title is a bit vague, I'll try to explain what I'm trying to do here.

I have a form that I can dynamically add/delete rows of a form from, and those rows need to generate some content in another table further down the page.  I need to be able to delete the entire contents of the generated table at any point from within the form, but I just can't seem to get it to work.

This is part of the form that is used to generate the table I spoke of, the voucherTable() function is where I need to perform the deleting.
<tbody id="glfNewRow">
<tr>
<td><input id="amount_1" type="text" name="amount_1" size="15" onBlur="voucherTable()"></td>
</tr>
</tbody>

This is the just the tbody tags for my content table, it begins empty and is populated as I go along.
<tbody id="glfVoucherValues">
</tbody>

Here is the portion of the voucherTable() function that I'm trying to perform the delete from.
function voucherTable()
{
var voucher_tbody = 'glfVoucherValues';
var voucher_table = document.all.glfVoucherValues;
//var voucher_table = document.getElementById(voucher_tbody);  // Tried a couple things here

while ((voucher_table = voucher_table.parentElement)  && voucher_table.tagName !="TBODY");
{
      voucher_table.parentElement.removeChild(voucher_table);
}
}

I guess because I'm trying to call this function from outside the tbody block I'm trying to delete, it doesn't like it.  At the moment I get an "Object Required" error on the removeChild line.

This is basically the same code I'm using to delete the rows in my form, only I get the voucher_table variable by doing voucher_table = window.event.srcElement; which works fine.  Of course, if I try to do that here it ends up deleting all of my form, which is kind of counter productive :)

Is this possible??

Thanks
0
Comment
Question by:souporman
3 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 11943443
I haven't used element handling a lot, but the code below doesn't work?
document.getElementById('glfVoucherValues').innerHTML = "";
0
 
LVL 15

Accepted Solution

by:
Thogek earned 350 total points
ID: 11943664
A couple of things about

    while ((voucher_table = voucher_table.parentElement)  && voucher_table.tagName !="TBODY");

What is the purpose of "voucher_table = voucher_table.parentElement"?  (Setting voucher_table to the original reference's parent?)  Or should the "=" be a "=="?

Should there be a semi-colon at the end of this line?  (That would cause the while block to end at the semi-colon, and the following line to be simply a line on its own, and not part of the while block.)

I'm guessing that you want something more like

    function voucherTable()
    {
        var voucher_tbody = document.getElementById('glfVoucherValues');
        var voucher_row;

        if(voucher_tbody && voucher_tbody.tagName.toUpperCase() == "TBODY")
            while(voucher_row = voucher_tbody.firstChild)
                voucher_tbody.removeChild(voucher_row);
    }
0
 

Author Comment

by:souporman
ID: 11943884
Thanks Thogek,

That works much better.

I'm fairly new at this javascript stuff, so I was really just throwing stuff at the code in hopes that something would work, which would probably explain some of the more confusing parts :)

Thanks again!
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Bootstrap Form 16 41
email validation 9 49
Adding 1 the value of input "text" every 3 seconds 5 33
Popup write two lines 3 22
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question