tags:

views:

61

answers:

2

Hello all,

In my web application, I was dynamically adding/deleting rows from my HTML table using jQuery.

I had a requirement that I needed to delete individual td elements(and not entire row).

My jQuery code is as follows:

$(function () {

    /* adding new row to existing table */
    // HTML template of a row
    $("#contactInfo").each(function () {
        $("button.addCommentRow", this).live('click', function () {

            var curr_row = $(this).closest('tr');
            var html = '<tr><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> ' + col_ninth + '</td> <td><button type="button" class="addCommentRow">+</button> <button type="button" class="deleteCommentRow">-</button> </td> </tr>';

            var newRow = $(html).insertAfter(curr_row); // add after the last
            newRow.find('.addCommentRow, .deleteCommentRow').button();
            newRow.find('.deleteCommentRow').button().click(function () {

                newRow.find("td:nth-child(9)").remove();
                newRow.find("td:nth-child(10)").remove();

            });
        });
    });
});

So, on deleting I wanted to remove the 9th and the 10th td element from my row.

The above code(only the deletion part) does not seem to work.
Any ideas/suggestions to get working will be highly appreciated.

Thanks,
Pritish.

A: 

I agree with Irwin. You probably want something like this:

newRow.find("td:nth-child(9)").html('');
newRow.find("td:nth-child(10)").html('');
William
This is *not* a good idea, you should use `.empty()` here, since you're leaking memory with `.html('')`
Nick Craver
I certain hope JavaScript would take care of the garbage collection there, or am I too optimistic? But I do agree that empty() is more elegant in this case.
William
@Nick : .empty() seems to work, except for one small thing though.I also want to make sure, if the 10th td element that I'm deleting, if it happens to the last one haviny any cell contents, then delete the tr also after that. The reason I say this, is with only .empty() of the individual td elements it's leaving behind row borders after deletions.
Pritish
@William - JavaScript won't cleanup the event handlers on `.deleteCommentRow` from `$.cache`, which `.remove()` and `.empty()` will :)
Nick Craver
@nick, here the guy want to `delete` the element. If `.empty()` is used it just makes the content of the element to **empty**, which means that its not going to delete the Element
Ninja Dude
@Avinash - Yes, but removing a `<td>` is a bad idea, it'll FUBAR the table layout in most cases, that's why emptying the cells would be a better approach, unless you're accounting for the lost cells in other ways.
Nick Craver
+1  A: 

you can try something like this, try the Demo : http://jsfiddle.net/HXB4Z/

HTML :

<table border=2>
    <tr>
        <td>jQuery</td>
        <td>mootools</td>
    </tr>
    <tr>
        <td>Dojo</td>
        <td>FUEL</td>
    </tr>
    <tr>
        <td>Raphael</td>
        <td>Rico</td>
    </tr>
    <tr>
        <td>SproutCore</td>
        <td>Lively Kernel</td>
    </tr>
</table>

javascript :

$(function() {
   $('tr')
       .find('td')
       .append('<input type="button" value="Delete" class="del"/>')
       .parent()//traversing to 'tr' Element
       .append('<td><input type="button" value="Delete row" class="delrow" /></td>');

    $('.del').click(function() {
       $(this).parent().remove(); //Deleting TD element
    });

    $('.delrow').click(function(){
       $(this).parent().parent().remove(); //Deleting the Row (tr) Element
    });
});
Ninja Dude