0
I’m having a reference problem in the click event, I don’t know what is avoiding the functionality of the jquery click function, here’s my code below for analysis:
div = $('#container_principal_box_pesquisa');
contador =1;
for(i=0; i<data.length; i++){
if(i < 5){
div.append('<div id="box_pesquisa_'+contador+'">'+data[i]+'</div>');
id = $('#box_pesquisa_'+contador);
div.find('img').width(60);
div.find('img').height(60);
contador++;
}
}
box1 = $("#box_pesquisa_1");
box2 = $("#box_pesquisa_2");
box3 = $("#box_pesquisa_3");
box4 = $("#box_pesquisa_4");
box5 = $("#box_pesquisa_5");
box1.click(function(){
alert('teste');
});
The click event of div’s box1...until 5 that are inside the container_principal_box_search do not work, even if I reference the click event of div whose id is the container_principal_box_search it works normally. What could it be? Someone can help me?
Thank you for the answer, I managed to solve it, it was very simple. Thank you very much for the help!
– Fábio Monteiro
I’m having the same problem, but wanted to solve it without jquery just javascript vanilla, how do I do it?
– The Coder
@Thecoder in your case, as you want to delegate an event to a dynamically created element and only use vanilla js, you can add an Event Listener to the Document and check the id or class of the element that was clicked example: https://jsfiddle.net/nk021zjv/
– Reynnan Viktor