Events cannot be superimposed on elements that do not exist when a function is developed. But you can create a wrapper element (div) around "class1" and "class2", for example:
<div id="class1_wrapper"> <span class="class1"> </div>
I used "span" for class 1 because I do not know what element it is. Then, instead of the click event, you can use "on ()":
$("#class1_wrapper").on("click", ".class1", function () {
this way, even if class1 does not exist (the same thing can be done for class 2), the click event will be fired
source share