Your $('.contracted').click(...)
code will bind a click handler to any elements with that class at the moment that code runs - this will not automatically work on other elements that have the class added later. If you want it to work on any li element that has the contracted
class at the time of the click use a delegated event handler set up via .on()
:
$('ul').on('click', 'li.contracted', function() {
...
});
This binds the click handler to all ul
elements, but then when a click occurs jQuery checks if the clicked item is a descendant of the ul
which matches the selector in the second argument and only calls your function if it does.