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?
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?
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
td
when you click on 1st td
td
when you click on 2nd td
td
when you click on 3rd td
example fiddle: http://jsfiddle.net/BmYue/2/
You can use -
$('td:last').on('click', function(){
$(this).siblings('td:first').css('color', 'red');
})
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/