0

I'm loading a part of a page using ajax with this script:

<script type="text/javascript">
    $(document).ready(function(){
        $('#btn_new').live('click',function(){
            var parameter = 1;
            $.ajax({
                type: "POST",
                url: "new_message.php",
                data: {
                        parameter:parameter,
                },
                success: function(msg)
                {
                    $("#lib_container").ajaxComplete(function(event, request, settings)
                    {
                        $("#lib_container").html(msg);
                    });
                }
            });
        });
    });
</script>

Inside the lib_container div I have a text input with this script:

<script type="text/javascript">
function searchUsers(str) {
        $.ajax({
            type: "POST",
            url: "users_ajax.php",
            data: {
                    search:str
            },
            success: function(msg)
            {
                //console.log(msg);
                //$("#users_lst").html(msg.response);
                $("#users_lst").ajaxComplete(function(event, request, settings)
                {
                    $("#users_lst").html(msg);
                });

            }

        });
}
</script>

When I load some text in the input, the second ajax script loads the msg variable in the div but then also reloaded the lib_container div. I've tried many ways to prevent the lib_container ajax trigger but it was in vain. Just putting the input in new page works correctly.

Can anyone help me solve this?

Thanks.

4
  • why are you setting an ajaxComplete callback when your ajax request is already done? when the success function runs that is the end of the request you already have your data no need to set ajaxComplete. Commented Jan 29, 2014 at 20:17
  • Every time you call .ajaxComplete() you're establishing another handler for all AJAX calls. Commented Jan 29, 2014 at 20:19
  • BTW, .live() was deprecated in jQuery 1.7, and removed in 1.9. Unless you're using an old version of jQuery, you should recode it using .on. Commented Jan 29, 2014 at 20:20
  • The first charge doesn't work without the ajaxcomplete. I don't know how to do the first charge differently, but I can't run this way the second Commented Jan 29, 2014 at 20:25

1 Answer 1

1

Your problem seems to be that you are registering a new ajaxComplete handler every time you get a successful response from the ajax call. Since it seems like you want to update the html, I'm not sure why you need those ajaxComplete handlers anyway.

Try it like this:

$(document).ready(function(){
    $('#btn_new').live('click',function(){
        var parameter = 1;
        $.ajax({
            type: "POST",
            url: "new_message.php",
            data: { parameter:parameter },
            success: function(msg) {
                $("#lib_container").html(msg);
            }
        });
    });
});

And by the same token:

function searchUsers(str) {
    $.ajax({
        type: "POST",
        url: "users_ajax.php",
        data: { search:str },
        success: function(msg) {
            $("#users_lst").html(msg);
        }

    });
}

On another note, .live is deprecated and you really ought to be using ajax promises rather than a success callback. See the jQuery api for more info on that.

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

1 Comment

Thanks, you've been very helpful. I deleted the ajaxComplete and it worked correctly

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.