Link to home
Start Free TrialLog in
Avatar of mike99c
mike99c

asked on

jQuery does not trigger on HTML loaded via AJAX call

I have a page which declares some jQuery code and I cannot get it to trigger on HTML which has been loaded using AJAX.

I have created the following simple code to illustrate the problem. It is a very simple page containing a link with a class called "link". There is also a div which will contain an external HTML file which also contains another link with the same "link" class applied to it. The jQuery triggers on the "link" class and pops up an alert.

It works when you click on "Click parent link" but not when you click on "Click loaded link" even though both links have the same class.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
</head>

<body>

    <script type="text/javascript">
    //<![CDATA[
    $(document).ready(function(){

        $('#internaldiv').load('link.htm');

        $(".link").click(function(){		
            alert("Link clicked");
            return false;
        });

    });
    //]]>
    </script>
    
    <a class="link" href="#">Click parent link</a>
    
    <div id="internaldiv">
    
    </div>

</body>
</html>

Open in new window


Here is the external link.htm file:

<a class="link" href="#">Click loaded link</a>

Open in new window


In the example given, the div loads on document ready, however it could equally load on some other event say clicking on a button or link.
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The ajax loaded page does not yet exist in the DOM when the parent page is "ready". By then the click event listener has already been assigned to the only existing class="link" on the page.

Consider this example where the click event listeners are not assigned until there are more than one class="link" elements present.
<script type="text/javascript">
    //<![CDATA[
    $(document).ready(function(){

        $('#internaldiv').load('link.htm');
		var test = setInterval(function(){
			if ($('.link').length > 1) {
				clearInterval(test);
				$(".link").click(function(){							
            		alert("#" + $(this).index() + " Link clicked");
            		return false;
				});
        	}
		}, 300);

    });
    //]]>
    </script>

Open in new window

That is an example of how not to use jquery.
Okay Gary, you have the better solution. That's why you are the Ace.
Nothing to do with the better solution, it is the solution. That is what .on is for
Your way is looping javascript until it can find two elements with the class, and then will never run again to capture any new links.
On top of that the click event won't even exist for the first link until there are two links.
I totally understand my mistake now. Thanks for elaborating. I've still got some learning to do. I won't give up my day job just yet.
Avatar of mike99c
mike99c

ASKER

Thanks Cathal, that did the trick. It's 01:14 in the morning here in London and I can now get some sleep.