Posting a non-news comment

I have a function for adding messages, where you can add messages, and you can comment on messages, the problem is commenting, it works great with existing posts, but when you add a new post and comment on this newly added post, it doesn’t work, that’s what I http : //jsfiddle.net/testtracker/Nh2NQ/
first check that the comments work fine with existing posts, and then add a post, now try commenting on this newly added post .. it doesn't work ... what's the problem. Help in search engines

thanks

+4
source share
4 answers

JQuery associates events with elements when the page loads, which is why when an event does not fire when new elements are added, because the event is not associated with them.

it will certainly work under any circumstances

$('#posts').on('submit', '.comment_entry form', function(e) { code to add comment..... }); 
+1
source

try the following: http://jsfiddle.net/Nh2NQ/5/

I changed this line

 $('.comment_entry form').submit(function (e) { }); 

in

 $('body').on('submit', '.comment_entry form', function (e) { ... }); 

therefore, using event delegation, you can attach a submit handler to dynamically inserted form elements. Feel free to change the body to any other common parent hierarchically "closer" to your elements

+2
source

This only adds a listener to the elements that are detected when evaluating the selector:

 $('.comment_entry form').submit( ... 

Your new form does not exist at the moment, and therefore the listener is not registered.

Use a live delegate instead:

 $('#posts').on('submit', '.comment_entry form', function(e) { ... }); 
+2
source

This is because jQuery triggers a submit event before your element exists. When you add a new .comment_entry form , jQuery is not aware of this, and the submit event is not associated with this particular element.

This link can help you .

0
source

All Articles