0

I'm using jquery to get a table of html search results via php based on a form variable and load it into a div. As the query can take a bit of time I'd like to displaying some form of loading anim but am not sure how to integrate it into my jquery.

Here's the code I have:

$(function() {
    $('#search_form').submit(function() {
        var data = $("#search_form :input").serializeArray();
        $('#results-panel').addClass('panel-primary');
        var model_id = data[0];
        $("#results").html('');
        $.get("/search/find_products/"+model_id.value, function (data) {
             $("#results").append(data);
        });
        return false;
    });
});

I'm still pretty new to jquery so this code is probably not the most efficient method. Any advice would be appreciated.

0

2 Answers 2

1

This is really straight forward using a .show() and .hide().

$(function() {
    $('#search_form').submit(function() {
        $('#myLoader').show();
        var data = $("#search_form :input").serializeArray();
        $('#results-panel').addClass('panel-primary');
        var model_id = data[0];
        $("#results").html('');
        $.get("/search/find_products/"+model_id.value, function (data) {
             $("#results").append(data);
             $('#myLoader').hide();
        });
        return false;
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can use fadeIn and fadeOut callbacks.

$(function() {
    $('#search_form').submit(function() {
        $("#loader").fadeIn(500, function() {
           var data = $("#search_form :input").serializeArray();
           $('#results-panel').addClass('panel-primary');
           var model_id = data[0];
           $("#results").html('');
           $.get("/search/find_products/"+model_id.value, function (data) {
                $("#loader").fadeOut(500, function() {
                    $("#results").append(data);
                });
           });
           return false;
    });
});

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.