+ 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

17th Feb 2018, 7:10 AM
Alfredo GirĂłn ZĂșñiga
Alfredo GirĂłn ZĂșñiga - avatar
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
17th Feb 2018, 7:38 AM
Burey
Burey - avatar
+ 4
what you can do is put the click event after the element creation in js
17th Feb 2018, 8:14 AM
Shardul Nalegave
+ 3
Thanks you all. I used Burey's solution, in order of a clear and ordered code. Event's delegation, đŸ€”đŸ‘đŸ‘đŸ‘.
17th Feb 2018, 2:58 PM
Alfredo GirĂłn ZĂșñiga
Alfredo GirĂłn ZĂșñiga - avatar