JQuery click function not working with data returned from ajax

I am working on a feature like updating a status message. The user can update the status message without refreshing the page, and a new message will be displayed after the user clicks the button, and other users can comment on the status message. The problem that I encountered is that when the user sends the data, a new status message will be displayed, and when I click the comment button, the comment window appears, but when I reload the page and click the comment button, the comment window is displayed .. .

+3
source share
1 answer

When you bind an event, the ajax html result does not exist yet.
JQuery has an easy way to handle this: Use live(or delegatein 1.4.2).

For example (from the above links):

instead:

$('.clickme').click(function() {
  // Bound handler called.
});

records:

$('.clickme').live('click', function() {
  // Live handler called.
});
+3
source

Source: https://habr.com/ru/post/1747833/


All Articles