Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude the last element in an unordered list using jQuery

I have an unordered list (ul) and a few list's elements (li).
I'd like to bind all the li's except the last one.

Example:

$('ul li').on('click', someFunction);

$('ul li:last-child').off('click');

Using jQuery, is there a way to do it in one line of code?
Any help will be appreciated!

like image 682
Lior Elrom Avatar asked Nov 20 '25 17:11

Lior Elrom


2 Answers

Yes, with the selector :not :

$('#elementName li:not(:last-child)').on('click', someFunction);
like image 109
Samuel Caillerie Avatar answered Nov 23 '25 07:11

Samuel Caillerie


This selector should work:

$('#elementName li:not(:last-child)').on('click', someFunction);
like image 24
tymeJV Avatar answered Nov 23 '25 06:11

tymeJV



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!