Solved

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

Posted on 2004-08-31
3
378 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how can i extend ? 2 43
jQuery delay animation 3 29
Is there a way to use PhantomJS on a shared server? 4 17
Writing comments on <p></P> or paragraph 2 13
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

773 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