tags:

views:

110

answers:

2

How do I check if an element is the last sibling?

For the last cell in a row I want to perform a different action.

This doesn't work:

$('td').each(function(){
    var $this = $(this);
    if ( $this === $this.parent().last('td') )
        {
            alert('123');
        }
})

And neither does it if I remove .parent().

+3  A: 

Try

$('tr td:last-child').each(function(){
    var $this = $(this);
    alert('123');
});
rahul
this works great, thankyou!
Haroldo
A: 

Here you go, but that only make sense if you want to do something to the other tds as well other wise use the last-child method described in one of the other answers.

$('td').each(function(){
    var $this = $(this);
    if ($this.index() == $this.siblings().length-1)
        {
            alert('123');
        }
})
ntziolis