This will add your class to all li that don't have children
$("#nav li:not(:has(li))").addClass('menu_last_child');
This will add a class to all li that have children
$("#nav li:has(li)").addClass("menu_parent");
Also keep in mind that there is a difference between :last and :last-child . The former will select only the last element in the returned set of elements matching the selector, while the latter will return all elements that are the last in the context of their parent element (same as the css :last-child selector)
source share