Loading jquery script after an event is done

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?

Upvotes: 1

Views: 123

Answers (1)

Kevin B
Kevin B

Reputation: 95030

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.

Upvotes: 2

Related Questions