javascript - How to assign click event for dynamically added span element? -
this question has answer here:
html:
<div class="testspandiv"></div>   javascript:
  $(document).ready(function () {       $('.testspandiv').append('<span id="testspan">hello</span>');   }    $('#testspan').on('click', function(){       $(this).parent().remove();   });   event not firing while giving click span element added dynamically. event firing if span element statically added html. can give suggestion on it.
i tried below also, not working.
  $('#testspan').on('click', '.testspandiv', function(){       $(this).parent().remove();   });      
you need use this:
  $('.testspandiv').on('click', '#testspan', function(){   // ^^ parent div             ^^ element in want bind function       $(this).parent().remove();  });      
Comments
Post a Comment