0

Update, answer found. Thanks @charlietfl for pointing me in right direction.

By implementing the following I caught the updated tags

<script>
    $("div#ItemInfo").on("click", "span.itemclass", function() {
        $('#timestamp').text($.now());
        });
</script>

Original question: I need some advice on how to get Jquery access to tags updated by php.

This is the code on main page

<!-- Native text with span. => Handled with native script-->  
<p> <span>Clickable</span>  <span>text</span></p>
<div id="timestamp"></div>

<!--    Content updated from data.php is put here. => Span in this content is NOT handled  by native script --> 
<div id='ItemInfo'></div>

<script>
 $("span").click(function(){
  $('#timestamp').text($.now());
  $('#ItemInfo').load('data.php?iid=4');
 });
</script>

As you see I update content within "ItemInfo" with content from another php page. The (span) tags within this code doesn't seem to be reachable from the native script, so I have to implement another script within the php code, which makes everything much more difficult, especially in regards of escaping and formatting.

echo "<script>                $(document).ready(function(){                          $(\"span\").click(function(){ var iid = $(this).attr(\"id\");                  $(\"#ItemInfo\").load(\"data.php?iid=\" + iid);                     });              });    </script>";

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    extract($row);
echo "<span id={$IID}>{$ITEM}</span><br />";

}

Basically I'd appreciate a way to handle the updates from the same script on the main page.

What is the best way to keep a Jquery script updated with all the elements on the page?

I don't know if this is best handled within Jquery or PHP but appreciate all tips on how to do this in best possible manner.

  • http://stackoverflow.com/questions/8110934/direct-vs-delegated-jquery-on @charlietfl - I was not sure to use the hammer since the question is very vague – mplungjan Jul 02 '16 at 16:19
  • Thanks for pointing me to a good answer. This solved my issues. By implementing the script like below, I caught the updated span tags – Peter Swift Jul 03 '16 at 06:08

0 Answers0