Access dynamically created items using jQuery?

后端 未结 6 1515
说谎
说谎 2020-12-19 13:13

I have a page where some html is being dynamically added to the page.

This is the html and javascript that is created:

6条回答
  •  一生所求
    2020-12-19 14:10

    Use .on to wire up the event to your button. Check this SO answer:

    Event binding on dynamically created elements?

    $(document).ready(function() {
        $('body').on('click', '#btn', function() {
            alert("Hello");
        });
    })
    

    Edit: I added the document ready code, you'll want to make sure you do that.

    Fixed.

提交回复
热议问题