[Answered ]-JQuery not working after ajax call, even though I'm using the correct on() method

2👍

This sounds like an application of jquery’s on method overload that uses the additional selector argument:

.on( events [, selector ] [, data ], handler )

From the jquery documentation:

When a selector is provided, the event handler is referred to as delegated. The handler is not called when the event occurs directly on the bound element, but only for descendants (inner elements) that match the selector.

So this should work:

$('body').on('click', '.upvotes', function() {
    $(this).css('color', '#fff'); 
});

Or in place of the ‘body’ selector use any other element that exists in the DOM at the time that javascript is executed.

Leave a comment