1
I’m trying to add an event click
to a class that is not initially present on the page, since it is added later when the user enters the data. The element in question is this:
<div class="tarefas" id="0">
<textarea class="titulo-ef" readonly="readonly"></textarea>
<textarea class="conteudo-ef" readonly="readonly"></textarea>
</div>
And I need to recognize the click
on the textarea.titulo-ef
, and as this is an element that whenever requested will be inserted a new one, I needed the click
recognized the interaction with the title, but the code does not work, currently it is like this:
$(".titulo-ef").on("click",function (){
alert("FUNCIONOU");
});
But like I said, I don’t get any warning.
The code that you said worked, but not with the ". tasks" I think it is because it is also added later, however I did with a more external relative of the scope and it worked perfectly, you can tell me if it is possible to somehow recognize which of the "title-E" was clicked by the user?
– Luan Sabino
You can add a specific class to each button and use the
$(this)
within the callback to make the distinction. As shown in my second example, I used the$(this)
to print the text of the button clicked - which is different between both buttons. That would be it?– Luiz Felipe
I think so, because as I will need to make changes with these Ivs, how to transfer from one to another I would need to identify the clicked "title-Ef" to transfer the content of this element to another div, by chance if instead of using a different class for each I use a different ID it will work the same way with $(this)?
– Luan Sabino
Yes. The
$(this)
is a normal jQuery object and you can do anything with it. Think about$(this)
as if it were the$('elemento-que-ativou-o-evento')
. You can get the ID as follows:$(this).attr('id')
. :)– Luiz Felipe
OK thank you so much for all the hints I will do everything possible to make it right here. thx :)
– Luan Sabino