user2301515
user2301515

Reputation: 5117

jquery events to generated html by javascript

How to do the next small thing:

<div id="mydiv">clickme</div>

If i click, "clickme" then appears "deleteme"

(function () {
    $(document).ready(function () {
        var initHtml = $('#mydiv').html();
        $('#mydiv').click(function (e) {
            $('#mydiv').html(initHtml + '<div>deleteme</div>');
        });
        $('#mydiv > div').click(function (e){
            $('#mydiv').html(initHtml);
        });
    });
})(jQuery);

How to put to work the deleteme? If I click it, it doesn't disappear.

Upvotes: 0

Views: 17

Answers (1)

KAD
KAD

Reputation: 11122

You will need to use event delegation with jquery on function since your division is dynamically created:

$(body).on('click','#mydiv > div',function (e){
     $('#mydiv').html(initHtml);
 });

Upvotes: 1

Related Questions