Skip to main content

jQuery Click Handlers (and React)

If you are working with a React component that is replacing old backbone/jquery stuff, you may encounter issues where existing jQuery click handlers stop working. This is often because the click handlers are created when the standard DOM is rendered and the React components may not exist yet.

To solve this, you can update the click handlers which are often something like this:

$('.yourClass').on('click', function....)

To something that targets the document instead and then you can put your class into the attributes instead of the selector like this:

$(document).on('click', '.yourClass', function....)

WARNING!!!
Do NOT put these click handlers in a loop function!!! Pay attention to where the code is at.