geekInside
geekInside

Reputation: 588

Remove element jquery with a specific id

I want to remove a specific row with a unique id when the user click on a delete img. It does not work when i select 'tr#'+id and then remove.

Here is my code

js

$("#page_body").on('click', 'a.delete_line', function(e) {
    var id=$(this).attr("id");
    $('tr#'+id).remove(); 
});

html

<tr id="unitesbudg_roic_assetcenter.V01.aaf.filename">
<td class="value">ANY</td><td class="value">MAIN </td>
<td class="value">false</td>
<td class="value">unitesbudg_roic_assetcenter.V01.aaf.filename</td>
<td class="value">unitesbudg_roic_assetcenter.txt</td>
<td class="value">undefined</td>
<td><a href="#" class="delete_line" id="unitesbudg_roic_assetcenter.V01.aaf.filename"><img src="img/delete.png"></a></td>
</tr>

Also if you can advice me on best practices for such a feature.

Thx a lot in advance

Upvotes: 0

Views: 99

Answers (3)

Mr.G
Mr.G

Reputation: 3559

Try this:

   $('#page_body tr:last').remove();

Upvotes: 0

Joshua Moore
Joshua Moore

Reputation: 24776

Your JQuery should look something more like this:

$("#page_body").on('click', 'a.delete_line', function(e) {
  $(this).closest('tr').remove();
});

Upvotes: 1

Satpal
Satpal

Reputation: 133403

In HTML IDs must be unique. You can use .closest() to find the parent tr

Just use

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

DEMO

Upvotes: 1

Related Questions