I have a link, myLink , which should embed the content loaded by AJAX in the div (appendedContainer) of my HTML page. The problem is that the click event that I linked to jQuery does not fire on newly loaded content that is inserted into appendedContainer. The click event is associated with DOM elements that do not load with my AJAX function.
What do I need to change for the event to be connected?
My HTML:
<a class="LoadFromAjax" href="someurl">Load Ajax</a> <div class="appendedContainer"></div>
My JavaScript:
$(".LoadFromAjax").on("click", function(event) { event.preventDefault(); var url = $(this).attr("href"), appendedContainer = $(".appendedContainer"); $.ajax({ url: url, type : 'get', complete : function( qXHR, textStatus ) { if (textStatus === 'success') { var data = qXHR.responseText appendedContainer.hide(); appendedContainer.append(data); appendedContainer.fadeIn(); } } }); }); $(".mylink").on("click", function(event) { alert("new link clicked!");});
Downloadable Content:
<div>some content</div> <a class="mylink" href="otherurl">Link</a>
javascript jquery html ajax jquery-events
confile May 16 '13 at 22:02 2013-05-16 22:02
source share