2

There has to be an easy solution to this, but alas, I can not find it.

My problem:

Basically I have some simple reporting software that uses PHP to pull information based on dates from a database and displays it inline using AJAX. Now that PHP script has also created links to click on that will simply display the previous weeks data or the next weeks data. I want those links to simply update the same div the data is originally or even a new div created from the php file, but using a different php script to handle the passed dates (or even the same one). It will not update the div from the created links after the first AJAX call. I have created some test files to keep it simple in figuring this out, but have had no luck.

Sample index.html:

<html>
<head>
<link rel="stylesheet" type="text/css" href="menu.css">
<link rel="stylesheet" type="text/css" href="format.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="ajaxmenucalls.js"></script>
</head>
<title>Test</title>
</head>
<body>
<a href="#" id ="innertest">Innertest</a>
<div id="div1"></div>
</body>
</html>

Sample AJAX script ajaxmenucalls.js:

    $(document).ready(function(){
        $("#innertest").click(function(){
            $.ajax({url: "innertest.php", success: function(result){
                $("#div1").html(result);
            }});
        });
        $("#innertest2").click(function(){
            $.ajax({url: "innertest.php", success: function(result){
                $("#div2").html(result);
            }});
        });
    });

Test file innertest.php (with php code removed / just html to test):

<a href="#" id="innertest2">Test 2</a><br /><div id="div2"></div>

My guess is that since the link with the id innertest2 isn't there upon my javascript file loading and appears after the first ajax call, it won't handle the click event of innertest2 the way I want it to.

So, is there a way to reload my javascript file in php directly after the element with an id of innertest2 is loaded so that it can handle it (if that's even the issue) or am I going about this completely wrong? I simply just want php to create a custom link inline that can also be clicked on to update information inline.

Phil
  • 157,677
  • 23
  • 242
  • 245
jfive
  • 56
  • 4
  • You should use event delegation to handle your click events on the dynamic content – Phil Feb 25 '15 at 23:36
  • That is exaclty what I was looking for and just didn't know the terminology to search. Thank you! – jfive Feb 25 '15 at 23:39

0 Answers0