Reputation: 451
My code creates dynamic divs. My function should work with these dynamic elements.
In HTML:
<div id="test">
</div>
In JQuery, I create few divs:
for ( var i = 0; i < 5; i++ ) {
$("#test").append("<div class=\"lists\" style=\"width=... height=...\"></div>");
}
In result, when I click on a div, an alert should be displayed. But this is not happening.
$(".lists").click(function () {
alert("Hi");
});
Upvotes: 0
Views: 50
Reputation: 121998
You have to use event delegation
$("#test").on("click", ".lists", function () {
alert("Hi");
});
Upvotes: 4