1

Maybe you understood from the title however here I go:

I have a file, from that file I load some other data from a php file:

File 1 html file

<html>
     <head>
      <script type="text/javascript" src="js/jquery.js"></script>
      <script type="text/javascript">
          $(function() {
               $('#content').load('source.php');

               $("#link").click(function() {
                       alert('Message1!');
                       return false;
               });
          });
      </script>
     </head>

     <body>
           <div id="content"></div>
     </body>
</html>

File source.php

<?php

for($i=1;$i<=10;$i++) {

     echo '<a href="#" id="link">Link '.$i.'</a>';

}

?>

After the content is loaded from the php file now this code

$("#link").click(function() {
        alert('Message1!');
        return false;
});

doesn't work for loaded content(links).

I need just an explanation how this DOM works, why loaded content can't interact with active functions/codes?

3 Answers 3

3

You should use live() or delegate() if you are using jQuery < 1.7

$("#link").live('click', function() {
        alert('Message1!');
        return false;
});

$("body").delegate('#link', 'click', function() {
        alert('Message1!');
        return false;
});

or use on() if you are using jqQuery > 1.7

$("body").on('click', '#link', function() {
        alert('Message1!');
        return false;
});

To handle events to DOM objects that are added after page load.

Remember that id should be unique on a page this is important. You could do:

<?php

for($i=1;$i<=10;$i++) {

     echo '<a href="#" class="addedLinks" id="link'.$i.'">Link '.$i.'</a>';

}

?>

and then use the class a selector

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

Comments

2

you just have to use live instead of click for example

$("#link").live('click', function() {
        alert('Message1!');
        return false;
});

EDIT :

I learned just now that it is deprecated since JQuery 1.7 so now we have to use .on() function like :

$("#link ").on("click", function(event){
    alert('Message1!');
    return false;
});

http://api.jquery.com/live/

http://api.jquery.com/on/

2 Comments

Thanks man, but the live method got deprecated on jQuery 1.7. Thanks also for the link that you attached :)
@Ghostology thanks for the heads up, it seems that now it is jquery .on() function which has semantically more meaning than .live()
0

One possible reason is becouse the ID should be unique for your elements in your page, but you are creating 10 of them:

for($i=1;$i<=10;$i++) {
    echo '<a href="#" id="link">Link '.$i.'</a>';
}

So add an index to the id for each element or make it class=link instead.

4 Comments

But it must work for the first link, when I load content it doesn't work in the first link neither.
I am not sure about this but i think it won't work at all test it again, and look at the other answers in this page that are about changing the click event will solve your problem or not
Yes MGA I tested your way and it worked on the first then. The .live was the solution however thank you too...
Sorry @Ghostology, I was trying to help but i wrote that becouse it is recommended to have a unique ID for each column in your page.

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.