0

I just want to remove the TR element from current delete image pressed...

I'm doing it:

$( "#btnDelete").click(function() {  
    $(this).parent().parent().remove();
});

See Fiddle

I can't see why it doesn't work

any tips?

RollRoll
  • 8,133
  • 20
  • 76
  • 135

5 Answers5

6

Only one element can have a given ID in an HTML document.

Use a class here and change your code to

$( ".btnDelete").click(function() {  
    $(this).parent().parent().remove();
});

or better (because more resilient to changes in your HTML) :

$( ".btnDelete").click(function() {  
    $(this).closest("tr").remove();
});

Demonstration

If your rows or buttons are dynamically added, use the delegation binding :

$(document).on("click", ".btnDelete", function() {  
    $(this).closest("tr").remove();
});

(instead of document, you may use any permanent element in which your buttons will be added)

Related answer

Community
  • 1
  • 1
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • thanks, if it is in the html document it works just fine, if it it is a dynamically created TR(by using .append(...) ) it doesnt. – RollRoll Dec 17 '14 at 18:06
1
$( ".btnDelete").click(function() {  
    $(this).closest("tr").remove();
});
Coder
  • 240
  • 2
  • 4
  • 20
1

Use a class instead and reference it as .btnDelete

code should change to

$('.btnDelete').click(function(){
    $(this).parents('tr').remove();
});
Cayce K
  • 2,288
  • 1
  • 22
  • 35
0

Well, it does work. The thing is you have given the same id for all three buttons. You should consider using a class for that purpose.

aldux
  • 2,774
  • 2
  • 25
  • 36
0

You should use classes for your buttons, because IDs have to be unique in your document and for dynamically added elements you have to use event-delegation:

$(document).on('click', '.btnDelete', function(){
    $(this).parents('tr').remove();
});

DEMO

empiric
  • 7,825
  • 7
  • 37
  • 48