1

I have view where I am adding dynamic content. I have added link on them (dynamic link). When click on it, not displaying alert.

I tried:

 $("a.reset_ebook").on("click", function() { alert("test") })

And view is:

<a ebook_id="18" class="reset_ebook" href="javascript:void(0)">Close</a>

How to alert with dynamic added link?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

4

Try this:

 $(document).on("click", "a.reset_ebook", function() { alert("test") })

This will listen for click events in the entire document but only trigger the handler if the target matches the selector (a.reset_book). That way it will also "work" for dynamically inserted elements.

Read more about direct and delegated events in the jQuery docs: http://api.jquery.com/on/#direct-and-delegated-events

David Hellsing
  • 106,495
  • 44
  • 176
  • 212
1

Your syntax will not work for delegated events

 $(document).on("click", "a.reset_ebook", function() { alert("test") })  

SEE HERE

PSR
  • 39,804
  • 41
  • 111
  • 151