views:

190

answers:

4
<tr>
 <td>#</td>
 <td>2009</td>
 <td><a class="delete_this">Click</a></td>
</tr>

I want to use jquery and get the text of 2nd (second) "td" when clicking the anchor. I want the "td" in the same tr as the anchor...

How do I do this?

So far I have

$(document).ready(function(){
   $(".delete_this').click(function(){
   var myNUmber = $(this).parent()....///And this i should write the code to get the text for second td in tr where the anchor belongs to
 })
})
+1  A: 

$('.delete_this').closest('tr').children(':eq(1)') .text();

1) Get the .delete_this A tag
2) Get the parent TR
3) Get the 2nd TD
4) Get the Text of the 2nd TD

Chacha102
delete_this is `<a>`. it has no siblings
ChssPly76
Was in the middle of revising
Chacha102
use .closest instead of .parents
redsquare
Done, and thanks.
Chacha102
.children(':eq(1)') would be better also, but the siblings answer below is less work
redsquare
+1  A: 
var myNUmber = $(this).parent().siblings().get(1).text();

Details are here

ChssPly76
+1  A: 

Your better adding just 1 click event by using .live rather than adding multiple click handlers, if you had a large table this will impact performance (think 100 separate bound events).

Also remember to prefix class selectors with nodeName if you can (here you are sure all delete_this are anchors)

 $('a.delete_this').live('click', function(){
   var myNUmber = $(this).parent().siblings().get(1).text();
 });
redsquare
+2  A: 

Here's a few ways:

$(this).parent().siblings("td:eq(1)").text()

If your looking for the cell before you can do it this way:

$(this).parent().prev().text()
drs9222