Skip to content Skip to sidebar Skip to footer

Select Tr By Id With Jquery

I'm trying select a tr inside a table to delete it but am not having any luck with selectors. Table looks like this: Copy

and the jQuery:

$('tr a').live('click', function () {
    $(this).closest('tr').remove();
});

another alternative to this selector would be

 $('tr[id^="product_"] a').live('click', function () {
    // you could ge the id number from the tr to do other things with var id = $(this).closest('tr').attr('id').replace("product_","");

    $(this).closest('tr').remove();
});

this would restrict it to only tr that whose id starts with "product_"

alternately you could delete item with an _id ending like this

 $('tr[id^="product_"] a').live('click', function () {
    // you could ge the id number from the tr var id = $(this).closest('tr').attr('id').replace("product_","");
    //then you could remove anything the that ends with _id 
    $('[id$="_'+id+'"]').remove();
});

I changed the code slightly here is a DEMO

Solution 2:

You don't need to have inline onclics and you don't need a function for this. All you need to do is is call this which refers to the item being clicked. In the below example, any item you click on will be removed.

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

Check working example at http://jsfiddle.net/aswae/2/

You can also insert a delete button and select to remove by button clicked.

Check updated example at http://jsfiddle.net/aswae/4/

Solution 3:

Your deleteProduct function takes an id argument, but nowhere in the onclick handler is that argument defined. Try this instead:

HTML

<!-- snip --><aonclick="deleteProduct(this)">

JavaScript

function deleteProduct(elt) {
    $(elt).closest('tr[id]').remove();
}

Demo 1

As pointed out in the comments, you've also got a bit of invalid markup in your <table> tag.


That said, you're using jQuery so there's no excuse for not writing unobtrusive JavaScript. Get rid of the onclick attributes entirely, and use this instead:

$('#selectedproducts a').live('click', function () {
    $(this).closest('tr[id]').remove();
});

Demo 2


If you want to get more specific with the selectors, this will work with the current markup:

$('#selectedproducts td:last > a').live('click', function () {
    $(this).closest('tr[id]').remove();
});    

Demo 3

Solution 4:

Change your [X] link to something like:

<aclass="delete">[X]</a>

And your jQuery to:

$(".delete").click(function() {
    $(this).parents("tr").remove();
});

Solution 5:

If you're literally using deleteProduct(id) you're going to need to replace ID with the number of that row.

You could also just hop up a few levels in the DOM tree (remove the parent's parent) instead of manually putting in IDs. I believe you could put onclick="$(this).parent().parent().remove()" instead.

Post a Comment for "Select Tr By Id With Jquery"

Between
's In Chrome

I am trying to achieve a simple table row hover effect by c…