tags:

views:

41

answers:

1

Hi

I'm trying to add and remove some rows from a table using Jquery. But I dont know how to remove the newly added rows. If I have a remove link it will work, but thats not what I want. I want to be able to call a js function that shall remove all items.

I have tried iterate over the items, but the count for the tr's in the table are always 0.

    <script src="scripts/jquery-1.3.2.min.js"></script>    
<script language=javascript>
    function UpdateData() {
        var dataStructure = new Object();
        dataStructure.Start = "Added Start";
        $("#table tbody tr:first").remove();        
        InsertDataInToTable(dataStructure);        
    }

    function InsertDataInToTable(dataStructure) {
        var newRow = $("#table thead tr:first").clone();        
        newRow.find("td").eq(0).text(dataStructure.Start);
        newRow.insertAfter("#table tbody");
    }

Start Inital start
A: 

I recommend trying to add the DOM elements manually and assign a class using addClass. Then you know which ones were added and which ones you want to remove.

Then doing $(".className").remove(); might be cleaner.

You could have them defaulted as a class 'standard' and using addClass('new') and remove the $('.new').remove();

Or you can just assign them all the .ClassName and remove all of that class name when you're ready.

I don't recommend using the prior recommendation of adding a fake attribute in the DOM like "newLine" because it's not valid XHTML. If you use .addClass() you can easily manage things in a validated way.

Hope this helps!

Joshua
Why are the new table rows not being counted in the DOM?
Robert Harvey
They are definitely counted. If you assign a class to them you will easily be able to select them. It might be the order at which you're calling your functions. The functions might be getting called too soon and your elements aren't available yet. Adding them to a particular class will reveal them easier for you.
Joshua
So checking `Document.Ready` before calling `remove()` ought to solve the problem then, right?
Robert Harvey