views:

53

answers:

8

Hello,

help me figure out how to set an event on the parent element (<tr>) I can't find the error in the condition

<script type="text/javascript">
$('table tbody tr').each(function(){
     $(this).find('a:first').click(function(){
        if($(this).parents().get(1).tagName == 'TR') {
         $(this).parents().get(1).find('tr').css('background', 'red'); //What's wrong?
        }
});
</script>

<table>
   <tbody>
      <tr>
         <td><a href="#">text</a></td>
         <td>text</td>
      </tr>
   </tbody>
</table>

Unfortunately, the formatting, I don't see any more special tag

A: 

Edit: This works:

$(function() {
  $('tr a:first').click(function() {
    $(this).parents('tr:first').css({background:'red'});
  });
});
sod
I don't think this will work. The parent is a `<td>`
Marko
yes, this will work, the problem is that, [.parents()](http://api.jquery.com/parents/) bubble up to the root. meaning, if this table are nested, all it's parents TR of `a:first` will be affected.
Reigel
+1 I agree with reigel. you need to add .get(0) after parents("tr") and wrap it as jQuery selector like `$($(this).parents('tr').get(0)).css...`
Ayaz Alavi
Nested tables? o.O Then just use parents('tr:first')
sod
+1  A: 
$('table tbody tr').each(function(){
     var $this = $(this);
     $this.find('a:first').click(function(){
         $this.css('background', 'red');
     });
});

crazy demo

Reigel
+1  A: 
$("table tr a:first").click(function() {
    $(this).closest("tr").css('background', 'red');
});
Marko
Thanks for this!
Algorithm
+1 it is a good approach.
Ayaz Alavi
A: 

Not completely sure about this, but I think that when you call $(this) inside the second function for clicking the a tag, $(this) is the a tag itself, and not the $(this) which is in the outer function the tr element.

o15a3d4l11s2
+2  A: 

hey you are missing closing }); for each statement. Here is the correct version of your code

$('table tbody tr').each(function(){
     $(this).find('a:first').click(function(){

           if($(this).parents().get(1).tagName == 'TR') {
               $($(this).parents().get(1)).css("background-color", 'red'); //What's wrong?
           }
      });       
 });

Working Example

EDIT: you can shorten above code very easily like assigning a class to anchor tag.

<a class="bindclick" href="#"></a>

$(".bindclick").bind("click", function(){
   var parent = $(this).parents("tr").get(0);
            OR
   var parent = $(this).closest("tr"); // http://api.jquery.com/closest/
   $(parent).css("background-color","red");
});
Ayaz Alavi
Many thanks to you
Algorithm
@Algorithm - Is this answer helped you, as with all your questions, [be sure to accept it](http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work).
Nick Craver
A: 

Many thanks to all

Algorithm
A: 
$('table tbody tr').each(function(){
          var $this = $(this);
          $this.find('a:first').click(function(){
             $this.css('background', 'red');
        });
});
Umakanta.Swain
A: 
$('table tbody tr').each(function(){
     $(this).find('a:first').click(function(){
        $(this).css('background', 'red');
     });
});

easy one.

Chouchenos