I want to bind all the li except the last one using jQuery. , is it possible to do that in one line,I've tried to Google it without any success.
$('#elementName li').on('click', someFunction);
$('#elementName li:last-child').off('click');
?
I want to bind all the li except the last one using jQuery. , is it possible to do that in one line,I've tried to Google it without any success.
$('#elementName li').on('click', someFunction);
$('#elementName li:last-child').off('click');
?
Share Improve this question asked Jan 12, 2015 at 13:36 user4445419user4445419 1-
1
$('#elementName li:not(:last)').off('click');
– Satpal Commented Jan 12, 2015 at 13:37
4 Answers
Reset to default 8You can use .not()
selector with :last
to filter out last element:
$('#elementName li').not(':last').on('click', someFunction);
or
$('#elementName li:not(:last)').on('click', someFunction);
You can use :not():
$('#elementName li:not(:last)').on('click', someFunction);
how about
$('#elementName li')
.not($('#elementName li:last-child'))
.on('click', someFunction);
You should use something like
$('#elementName li:not(:last-child)').on('click', yourFunc);