Attach event to dynamic elements in javascript

前端 未结 10 2409
一生所求
一生所求 2020-11-21 23:16

I\'m trying to insert html data dynamically to a list that is dynamically created, but when i try to attach an onclick event for the button that is dynamically created the e

10条回答
  •  我在风中等你
    2020-11-21 23:33

    There is a workaround by capturing clicks on document.body and then checking event target.

    document.body.addEventListener( 'click', function ( event ) {
      if( event.srcElement.id == 'btnSubmit' ) {
        someFunc();
      };
    } );
    

提交回复
热议问题