Bind all but the last

I want to bind all li except the last using jQuery. whether it is possible to do this in one line, I tried to do this without any success.

$('#elementName li').on('click', someFunction);
$('#elementName li:last-child').off('click');

?

+4
source share
4 answers

You can use the .not()c selector :lastto filter the last element:

 $('#elementName li').not(':last').on('click', someFunction);

or

 $('#elementName li:not(:last)').on('click', someFunction);
+9
source

You can use : not () :

$('#elementName li:not(:last)').on('click', someFunction);
+8
source

What about

$('#elementName li')
    .not($('#elementName li:last-child'))
    .on('click', someFunction);
+6
source

You should use something like

$('#elementName li:not(:last-child)').on('click', yourFunc);
+1
source

Source: https://habr.com/ru/post/1570878/


All Articles