javascriptjqueryajaxjqueryform

Return random order of list items on form submit


I am submitting a search form via ajax and returning the results from another page into a ul element. I am wondering how to return the li elements in a random order. I am using something called jquery form to get this done.

AJAX:

$(function() {
    $("#js-form").ajaxForm({
        success: function(data, txtStatus, jqXHR) {
            alert(data);
            $("#js-results").empty();
            $(data).children("li").appendTo("#js-results");
        }
    });
});

Solution

  • Using the shuffle function:

    $(function() {
        $("#js-form").ajaxForm({
            success: function(data, txtStatus, jqXHR) {
                var resultsObj = $("#js-results");
                resultsObj.empty();
                $(shuffle($(data).children("li"))).appendTo(resultsObj);
            }
        });
    });