jQuery: how do I check if an element is the last sibling?

Solution 1:

This is more elegant and worked for me:

if($(this).is(':last-child'))
{
    alert('123');
}

Solution 2:

Try

$('tr td:last-child').each(function(){
    var $this = $(this);
    alert('123');
});

Solution 3:

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');
        }
})

Solution 4:

you can code this way.

var $this = $(this);
if($this.index()==$this.siblings().size()-1)
{
   alert("It's the last.");
}