0

I have a link on my page, that if clicked, will generate a popup via Ajax. (The popup code is not under my control)

Now i want to Add functionality, via my code jQuery, on a link of this popup.

This popup is not printed in source code after page is loaded. Obviously the jQuery can't find this link.

How can i do, to add a click() trigger functionality on this link in this ajax popup?

Vivek Kodira
  • 2,764
  • 4
  • 31
  • 49
Relab
  • 1
  • 1

1 Answers1

1

If I properly understood the problem, your click event is not fired because the target is loaded after the event is attached. To fix it, you can un the `[on]'(http://api.jquery.com/on/) method.

$("#myModal").on( "click", "a#myLink", function() {
  console.log( "click fired");
});

$("#modalContainer").on( "click", "#myTargetElement", function() 
{
  console.log("click fired");
});

$('#btn').click(function()
               {
  $('#modalContainer').html("<div id='myTargetElement'>ajax loaded content</div>")
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="modalContainer">initial content</div>
<button id=btn>fake ajax load</button>
Mathieu VIALES
  • 4,526
  • 3
  • 31
  • 48