5
I created several div
by using jQuery to assign them the CSS class numero
:
for(...)
textoHTML += "<div class='numero par'>"+sorteado+"</div> <!-- numero par-->\n";
...
textoHTML += "<div class='numero impar'>"+sorteado+"</div> <!-- numero ímpar-->\n";
$( "#quadro" ).html(textoHTML);
I want to be able to click this div
and recover the drawn number.
But I still don’t know how to do it, it’s with the innerHtml
? this
?
However, it seems that the mouse click event is not working:
$( "numero" ).click(function() {
console.log("Clicou no número");
});
With the above code nothing happens. It may have something to do with the fact that the CSS class numero
of div
created is not originally on DOM?
Thanks for the refactor à la @Zuul (TM)
– Sergio
Sergio sometimes working with ". on" in this way: "$("#div"). on('click',Function(){ " didn’t work for me and only using " $(Document). on("click","#div",Function(){ " worked. Do you know why that is? You think it’s worth asking a question about?
– Joao Paulo
@Joaopaulo, take a look at this answer: http://answall.com/a/5199/129
– Sergio
When you use the ID it only takes the first element "Selects a single element with the Given id attribute". When you add the event to the Document and use the internal ID selector it manages to map all the elements, but "weighs" keep adding so many events in the Document.
– Leandro Amorim