0

I want to access the sibling of a column :

   <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
   </tr>

When i click over 3, how can i access 1?

Gaurav
  • 8,367
  • 14
  • 55
  • 90

4 Answers4

1

You can use .siblings jquery function

http://api.jquery.com/siblings/

sv_in
  • 13,929
  • 9
  • 34
  • 55
1

If I well understood;

var td = $('tr td'), len = td.length;
td.on('click', function() {
    var i = (td.index(this) + 1) % len;
    console.log(td.eq(i));
});

this will return

  • 2nd td when you click on 1st td
  • 3rd td when you click on 2nd td
  • 1st td when you click on 3rd td

example fiddle: http://jsfiddle.net/BmYue/2/

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
1

You can use -

$('td:last').on('click', function(){
    $(this).siblings('td:first').css('color', 'red');
})

Demo: http://jsfiddle.net/RK56q/

Dipak
  • 11,930
  • 1
  • 30
  • 31
0

or you could use .parent() and .children() method as well to access it.

$(this).parent().children()

But easier version is .siblings jquery function.

http://api.jquery.com/parent/ http://api.jquery.com/children/

Ravi
  • 3,132
  • 5
  • 24
  • 35
  • $(this).parent().siblings(":first").html() i tried this , but not working – Gaurav May 31 '12 at 13:49
  • 1
    when you refer $(this) it refers to td element so try this. you could access directly as $(this).siblings(":first").html() or else use $(this).parent().children(":first").html() – Ravi May 31 '12 at 13:52
  • This won't help with your main problem, but I notices that you don't need the first selector because .html() only returns the value for the first element in a list – Grezzo May 31 '12 at 13:54