What you can assign to document
is a click
event in your class, since elements are being generated after DOM
has already been loaded, so it will not catch the click
event, do as follows:
$(document).on('click', '.option', function(){
//Evento do click
})
This way you say that whenever you click on document
on this classe
it executes this method!
Another way is to use jQuery's delegate (), this way you say that in every element (div) in class (option1) it will execute this method
$('div').delegate('.option1','click', function() {
//Evento do click
});
I added 3 examples separated by a line, check that in the first example with each click it generates an element, but the generated element has no action, already in the second and third examples the generated elements have the desired action:
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script><divclass="conteudo1">
<div class="option1" data-option="1">Click1</div>
</div>
<hr>
<div class="conteudo2">
<div class="option2" data-option="1">Click1</div>
</div>
<hr>
<div class="conteudo3">
<div class="option3" data-option="1">Click1</div>
</div>
<script>
$('.option1').on('click', function () {
var option = $(this).data('option');
$('.conteudo1').append("<div class='option1' data-option='"+(option+1)+"'>Click"+(option+1)+"</div>");
});
$(document).on('click','.option2', function () {
var option = $(this).data('option');
$('.conteudo2').append("<div class='option2' data-option='"+(option+1)+"'>Click"+(option+1)+"</div>");
});
$('div').delegate('.option3', 'click', function(){
var option = $(this).data('option');
$('.conteudo3').append("<div class='option3' data-option='"+(option+1)+"'>Click"+(option+1)+"</div>");
})
</script>