Having received the table table $ this parent siblings table!

I have a href link under my table and I want to be able to manipulate the rows of the table by clicking on this link, but I can’t get them!

here is my html

<div> <div> <a href="#remove" class="removelink" > remove </a> </div> <table> <tr> <td></td> </tr> </table> </div> 

I want to do something like:

 $('.removelink').click(function(){ $(this).parent().siblings('table tr:last').remove(); }) 

I can get to the table

 $(this).parent().siblings('table') 

but i cant get the lines with something like

 $(this).parent().siblings('table tr') 
0
source share
3 answers

You can use find , to go to the tr of table :

 $('.removelink').click(function(){ $(this).parent().siblings('table').find('tr:last').remove(); }); 

Here is a working example . If your HTML structure is always exactly as you showed, you can use next() instead of siblings('table') for a bit shorter code.

The problem with your current code is that siblings('table tr') will look for a sibling of the div , which is tr , and there are none!

+2
source

.siblings(selector) will return all siblings of a particular element that match the selector.

.siblings('table tr') will return only something if the context element has tr elements like siblings, but the div does not.

Just use .find :

 $(this).parent().siblings('table').find('tr').last() 
+1
source
 var $context = $(this).parent().siblings('table'); $("tr:last", $context); 
0
source

All Articles