1

##### SOLVED ##### I had a javaScript refresh setInterval("...", 1000); in the source code which caused the error. Thanks a lot for your help!

the html

<div class="stackwrapper" id="user1"></div>
<div class="stackwrapper" id="user2"></div>
<div class="userdrawings"></div>

the javascript

$('.stackwrapper').click(function(e){
var id=$(this).attr('id');
$('.userdrawings').load('loadSession.php?user='+id).fadeIn("slow");
});

Somehow it only works at once, only at the first click on stackwrapper, when I click on the second one, the function is not triggered again.

3
  • Do you get an error message in your console? Commented Mar 23, 2012 at 11:07
  • It looks like there is something else firing an error. Does the response contain JavaScript code that fails (load() executes containing JavaScript by default)? Commented Mar 23, 2012 at 11:39
  • 1
    If you have found your own solution, you should post it as an answer, then accept it, so that others may more easily find it. Commented Aug 14, 2012 at 20:51

5 Answers 5

1

Okay now i get it, it's because you're making ajax call. Here's a link that answers your question.

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

3 Comments

But this is not talking about what is happening here as the bound elements are not changing: When you call $('a'), it returns all the links on the page at the time it was called, and .click(fn) adds your handler to only those elements. When new links are added, they are not affected. See the AJAX and Events Tutorial for a longer discussion.
There are only 2 "stackwrapper" classes? i have a solution. after .load you call a function. function2() .. and you create the function you put the same code in it. $('.stackwrapper').click(function(e){ var id=$(this).attr('id'); $('.userdrawings').load('loadSession.php?user='+id).fadeIn("slow"); });
What the FAQ is talking about is that when you load content via AJAX the newly created elements will not be automatically bound to event handlers that are declared on $(document).ready() - This is not what seems to go wrong here
0

try to put it inside a

$(document).ready(function(){
//place the above code here.
});

Comments

0

Can't reproduce the problem with this self-contained example

<?php
if ( isset($_GET['user']) ) {
    echo '<span>user=', htmlspecialchars($_GET['user']), '</span>';
    die;
}
?>
<html>
    <head>
        <title>...</title>
        <style type="text/css">
            .stackwrapper { width:100px; height: 100px; margin: 5px;  background-color: red }
        </style>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('.stackwrapper').click(function(e) {
                    var id=$(this).attr('id');
                    $('.userdrawings').load('?user='+id).fadeIn("slow");
                });
            });
        </script>
    </head>
    <body>
        <div class="stackwrapper" id="user1"></div>
        <div class="stackwrapper" id="user2"></div>
        <div class="userdrawings"></div>
    </body>
</html>

You might want to use a javascript debugger, like e.g. included in firebug and check for errors.

1 Comment

Thanks a lot, found the mistake!
0

You could try putting it at the document level:

$(document).on('click', '.stackwrapper', function(e) {
    var id = $(this).attr('id');
    $('.userdrawings').load('loadSession.php?user=' + id).fadeIn("slow");
});

Comments

0

It's strange , while It works well on my computer . It changes every time when I click. this is my code

html

<div class="stackwapper" id="user1">user1</div>
<div class="stackwapper" id="user2">user2</div>
<div class="userdrawings"></div>

js

$(document).ready(function(){
$(".stackwapper").click(function(e) {
       var id = $(this).attr('id');
       $(".userdrawings").load("user_session.php?user="+id).fadeIn("slow");
    });
});

user_session.php

$user=$_GET["user"];
echo "hello " . $user;

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.