Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

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().

like image 492
Haroldo Avatar asked Apr 21 '10 09:04

Haroldo


People also ask

How can I find previous siblings in jQuery?

jQuery prev() Method The prev() method returns the previous sibling element of the selected element. Sibling elements are elements that share the same parent. The DOM tree: This method traverse backwards along the previous sibling of DOM elements.

Is last child jQuery?

It is a jQuery Selector used to select every element that is the last child of its parent. Return Value: It selects and returns the last child element of its parent.

Is jQuery the last element?

The last() function is an inbuilt function in jQuery which is used to find the last element of the specified elements. Here selector is the selected elements. Parameters: It does not accept any parameter. Return value: It returns the last element out of the selected elements.


1 Answers

This is more elegant and worked for me:

if($(this).is(':last-child')) {     alert('123'); } 
like image 185
bbeckford Avatar answered Oct 03 '22 12:10

bbeckford