+ 2
The jQuery issue.
Hi, I'm trying to handle the click event on a class. It works fine but only with the elements of the HTML file. In the JS file I create new elements with the same class (clicking a button), but they don't work properly. Thanks for your help and support. https://code.sololearn.com/WIqK37O0UQtx/?ref=app
3 Answers
+ 8
change your click handler from this
$(".bye").on("click", function(){
$(this).parent().remove();
});
to this
$(document).on('click', ".bye", function() {
$(this).parent().remove();
});
explanation is here
https://stackoverflow.com/questions/16893043/jquery-click-event-not-working-after-adding-class/16893057
+ 4
what you can do is put the click event after the element creation in js
+ 3
Thanks you all. I used Burey's solution, in order of a clear and ordered code. Event's delegation, đ€đđđ.