1

I have function that should be triggered on click and there is two different elements that can do that. One element is static created on page load, and the second element is dynamic. I'm wondering if there is a way to combine this in single statement with JQuery. Here is example:

$('.show_new').on('click',showElement);
function showElement() {
  $('#test').append("<input type='button' id='dynamic' class='save_form' value='Save'>")
}

$('.save_form').on('click',testFun);
$(document).on('click','.save_form',testFun);
function testFun(e) {
  alert(e.target.id);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="button" class="show_new" value="Show">
<input type="button" class="save_form" id="static" value="Save">
<div id="test"></div>

As you can see in example above the only way for this to work is to have to lines one for static and one for dynamic element event. If anyone knows better way of doing this please let me know. Thanks!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
espresso_coffee
  • 5,980
  • 11
  • 83
  • 193

0 Answers0