myRM
myRM

Reputation: 249

Delete the table row of the Last Triggered Button jQuery

I want to delete the row where the triggered button is. Every row has a button that calls a div or form. After the event on that div or form by clicking back, row should be deleted.

JSFIDDLE

$('#btn1, #btn2').click(function() {
    $('#myDiv').show();
    $('#myTable').hide();   
});

$('#bck').click(function() {
    $('#myDiv').hide();
    $('#myTable').show();
    alert('DELETE THE ROW OF THE LAST TRIGGERED BUTTON, IF THE USER CLICK Btn 1 THE ROW WITH BTN 1 WILL BE DELETED');
})

;

Upvotes: 4

Views: 81

Answers (2)

Tats_innit
Tats_innit

Reputation: 34117

Demo http://jsfiddle.net/DmWNz/ or http://jsfiddle.net/Sr28Q/

What I did: (for your specific need)

  • Store the id of the button.
  • Extractnumeric value form the id and hook to the rowid -- .replace(/[^\d]/g, '')
  • To remove use .remove() api : http://api.jquery.com/remove/

please note In the HTML the id of the 2 buttons were same, they are fixed as well.

Rest should fit the need. :)

code

var rowId;

    $('#btn1, #btn2').click(function () {
        $('#myDiv').show();
        $('#myTable').hide();
        rowId = $(this).prop('id');
    });

    $('#bck').click(function () {
        $('#myDiv').hide();
        $('#myTable').show();
        $('#myRow' + rowId.replace(/[^\d]/g, '')).remove();
        alert('-- ' + rowId);
    });

Upvotes: 1

chasew
chasew

Reputation: 8828

Here's one approach using a static variable:

window.btnId = $(this).attr("id");

and the closest function

$("#"+window.btnId).closest("tr").remove();

http://jsfiddle.net/M3W2L/4/

Upvotes: 1

Related Questions