Search code examples
jqueryhtmldomjquery-selectorscss-selectors

How to check if the element is not the first-child?


How do you know if the current element is not the first-child?

It should work with $(this), for example:

$("li").click(function(e) {
    if (/* $(this) is not the first-child */)
    {
        /* do something */
    }
});

Solution

  • You can do this just to test an element if it's the first child: $(this).is(':first-child'). Other selectors like :last-child would work too.