Skip to content Skip to sidebar Skip to footer

Accessing Siblings In Jquery

I want to access the sibling of a column : 123 When i click over 3, how c

Solution 1:

You can use -

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

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

Solution 2:

You can use .siblings jquery function

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

Solution 3:

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/

Solution 4:

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/

Post a Comment for "Accessing Siblings In Jquery"