1

Easy for medium and initiated users. Given the following files:

html:

<html>
  <head>
     <script>
       $(document).ready(function()
       {
          $("#fashion").onclick(function()
          {
             $get("ajax.php", { section: fashion}, function(result)
             {
                $("#container").html(result);
             });
           });

           $("div").click(function()
           {
             var identification = $(this).attr("id");
             alert(identification);
           });
        });
     </script>
  </head>
  <body>
     <div id="fashion">
       Click me
     </div>

     <div id="container">
     <div>

     <div id="test">
       Test
     </div>
  </body>
</html>

I retrieve html information from another page "ajax.php" called previously in the method

<?php
if($_GET['fashion'])
{
   ?><div id="fashion_content">This is a random text</div><?php 
}
?>

The thing is that with the function $("div").click(function... in the script section won't return the id from the div generated with the ajax get method (div id="fashion_content") but only the others("fashion","container","test"). On the other hand when I put the jquery function after the div in the ajax.php file as following it works:

<?php
if($_GET['fashion'])
{
   ?><div id="random">This is a random text</div>

     <script>
           $("div").click(function()
           {
             var identification = $(this).attr("id");
             alert(identification);
           });
     </script>
    <?php 
}
?>

I guess I have to reload the script (or only the function) after the get event is completed. But how?

1 Answer 1

2

try

$("div").live('click',function()
  {
    var identification = $(this).attr("id");
    alert(identification);
});

also, that can go outside of the $(document).ready() to speed up execution

With this snippet, your php wont need to return the script adding the click event.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.