benhowdle89
benhowdle89

Reputation: 37464

jQuery can't remove dynamically added row

Is there something stopping this working, I'm dynamically adding a row to a table, then if they click the new row (or any row in the table) it should disappear...but it isnt working?

$('.addtocart').click(function(){
                    var omPartNo = $(this).next().text();
                    var supPartNo = $(this).next().next().text();
                    var cat = $(this).next().next().next().text();
                    var desc = $(this).next().next().next().next().text();
                    var manuf = $(this).next().next().next().next().next().text();
                    var list = $(this).next().next().next().next().next().next().text();
                    var disc = $(this).next().next().next().next().next().next().next().text();
                    var priceEach = $(this).next().next().next().next().next().next().next().next().text();
                    $('#cart table').append('<tr class="tableRow"><td><a class="removeItem" href="#"><img src="/admin/images/delete.png"></img></a><td>' + omPartNo + '</td><td>' + supPartNo + '</td><td>' + cat + '</td><td>' + desc + '</td><td>' + manuf + '</td><td>' + list + '</td><td>' + disc + '</td><td>' + priceEach + '</td></tr>');
                });

                $('.tableRow').click(function(){
                    $(this).remove();
                });

Upvotes: 1

Views: 2072

Answers (1)

KeatsKelleher
KeatsKelleher

Reputation: 10191

Use live, like this:

$('.tableRow').live('click',function(){
    $(this).remove();
});

Upvotes: 5

Related Questions