klvler klvler - 1 year ago 118
Javascript Question

javascript, jQuery: How to re-listen for click events

I am listening for click events on the class



Works fine. The problem is, if I append a new Element with the class
to the DOM, jQuery doesn't listen for it = if you clicked on the appended Element, nothing happens.

How can I fix that?

Answer Source

instead of



$(document).on("click", ".notifications-link", function(){...});

this way you're attaching the event to the document and then checking to see if the click landed on ".notifications-link" and will work will all newly added ".notifications-link"'s

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download