Tuesday, October 29, 2019

javascript - jQuery - dynamically generated children dont trigger click




my code:



$(".test1").children().click(function(){
$(".test2").append('something');

});

$(".test2").children().on('click', function(){
alert("done");
});


the span with "something" should be the last child of test2 after clicking children of test1.
is my append done right? is the click event on test2 done correctly?




thx!


Answer



this works:



$(".test1").children().click(function(){
$(".test2").append(' something');
});

$(".test2").on('click', 'span', function(){
alert("done");

});


live demo


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...