0

I have a div with a button inside that must update the same div...

Example

<div id="result">
  <a href="#" class="item" data-value="ok">click</a>
</div>

$( "a.item" ).click(function() {
  val = $(this).data("value");
  alert(val);
  $("#result").html('<a href="#" class="item" data-value="ok">click</a>');
});

but the button works only first time

This is jsfiddle https://jsfiddle.net/d8w4nsev/

FireFoxII
  • 828
  • 4
  • 18
  • 31
  • 1
    use dynamic delegation un `.on()` like `$( "#result" ).on("click","a.item",function() {` – guradio Dec 12 '17 at 00:24
  • Hi check below question/answer https://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements as you're updating content of the div (button itself) the "event binding" gets lost after first click, because inserted html is "new" and jQuery isn't tracking the updated DOM. – Allende Dec 12 '17 at 00:26

0 Answers0