Reputation: 1200
I want to generate some code on client side using jquery by setting its html contents using .html()
to a specific id. Here's the example to show what I am doing:
For my class chat-links
, it will alert hi if I coded it as such under HTML, but it would not
work if I generate it by setting the same line of code under another the html content id of
chat-test
. My question is, how can I generate that line of code, set it under the id chat-test
in this case, and would still be able to identify it with the class name chat-links
instead of chat-test
? Or is there a way so that I can change the id of chat-test
into chat-links
?
So basically, I want it to be able to alert hi when I click def.
Thanks.
Upvotes: 0
Views: 80
Reputation: 207891
Since you're adding elements to the DOM dynamically, you need to use .on()
instead of .click()
to delegate the event handling.
$(document).on('click', '.chat-links a', function () {
alert("hi");
});
Upvotes: 5