user516883
user516883

Reputation: 9378

Removing row that button is clicked on using jquery

I have a table with rows in it. When I click a button it is removes the row prior to it. I am using the closest() function to determine the row. I would like to delete the same row that the button is in.

function DeletClick(id, date) {
    var e = window.event; \
    if (confirm('Are you sure you want to delete the record from ' + date + '?')) {
        DisplayInformation.Delete(id,
           function (result) {
               if (result) {
                   jQuery('#' + e.srcElement.id).closest('tr').remove(); //Delete Row here!
               } else {
                   alert('You Do not have permssion to delete record.');
               }
           });
    }
   }

Upvotes: 0

Views: 120

Answers (1)

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

If the button is inside the td of the specific row, then you can use the same logic to determine the current row,

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

Assuming DeletClick function is the click handler for the delete button.

In case if you have binding the event handler inline of the button HTML like below then you need to pass the this as an arg to the function and update the this in the above code with that argument.

<input type="button" value="Delete" onclick="DeletClick('someid', 'somedata', this)" />

Then inside the function,

function DeletClick(id, date, thisObj) {
    var e = window.event; \
    if (confirm('Are you sure you want to delete the record from ' + date + '?')) {
        DisplayInformation.Delete(id,
           function (result) {
               if (result) {
                   jQuery(thisObj).closest('tr').remove(); //Delete Row here!
               } else {
                   alert('You Do not have permssion to delete record.');
               }
           });
    }
}

Upvotes: 1

Related Questions