Determining if an element is the last child of its parent

Using jQuery, is there a quick way to find out if an element is its last parent child?

Example:

<ul> <li id="a"></li> <li id="b"></li> <li id="c"></li> </ul> $('#b').isLastChild(); //should return FALSE $('#c').isLastChild(); //should return TRUE 
+54
jquery
Nov 17 2018-10-17T00:
source share
3 answers

Use : last-child selector with .is ()

 $('#c').is(':last-child') 
+109
Nov 17 '10 at 22:04
source share
β€” -

You can use .is() with :last-child as follows:

 $('#b').is(":last-child"); //false $('#c').is(":last-child"); //true 

Here you can test it . Another option is to check .next().length if it is 0 is the last element.

+15
Nov 17 '10 at 22:04
source share
 if($('#b').is(":last-child")){ } 
+5
Nov 17 '10 at 22:04
source share



All Articles