First I tried like this:
$('.choose-car li:not(.selected)').click(function () {
console.log('works and with selected');
$(this).addClass('selected');
});
Then I thought a litle and and tried like this:
$('.choose-car li:not(.selected)').on('click', function () {
console.log('works and with selected');
$(this).addClass('selected');
});
both works with selected..
But now I thinking maybe I have to remove click event after first click or is there easier solution? Maybe I missing something?
Your code is adding the
selectedclass after you register the handler. This is probably not what you want, since the handler will remain bound to the element even after it acquires theselectedclass.Try delegating the event instead: