bland_dan
bland_dan

Reputation: 446

JQuery AJAX problem

If you look at http://www.danfarrellwright.com/screwsline/front_end/product.php?product_id=104 I have written the following function to add the table headers in every five rows:

jQuery.fn.hrReplace = function() {
    var headers = $(this).find('table.products tr:eq(0)');

    $(this).find('table.products tr:nth-child(5n)').after(headers);
    $(this).find('table.products tr:eq(0)').before(headers);
}

This works fine on loading the page. I have then added the function during the ajax command:

$.ajax({
    type: "POST",
    url: formAction,
    data: myForm,
    success: function() {
        $('#top_bar').load(base + 'index.php #top_bar');
        $('#' + divId).load(document.URL + '&random=' + Math.random() * 99999 + ' #' + divId, {
            'gaugeMinFil':parseFloat($('input[name="gaugeLow"]').val()),
            'gaugeMaxFil':parseFloat($('input[name="gaugeHigh"]').val()),
            'lengthMinFil':parseFloat($('input[name="lengthLow"]').val()),
            'lengthMaxFil':parseFloat($('input[name="lengthHigh"]').val())
        });
        $('#tableHolder').hrReplace();
    }
});

But after reloading the div hrReplace() isn't working.

Upvotes: -1

Views: 66

Answers (1)

Fosco
Fosco

Reputation: 38526

The function call needs to be in the callback for the .load call, so it happens after it completes.

$.ajax({
    type: "POST",
    url: formAction,
    data: myForm,
    success: function() {
        $('#top_bar').load(base + 'index.php #top_bar');
        $('#' + divId).load(document.URL + '&random=' + Math.random() * 99999 + ' #' + divId, 
           {
            'gaugeMinFil':parseFloat($('input[name="gaugeLow"]').val()),
            'gaugeMaxFil':parseFloat($('input[name="gaugeHigh"]').val()),
            'lengthMinFil':parseFloat($('input[name="lengthLow"]').val()),
            'lengthMaxFil':parseFloat($('input[name="lengthHigh"]').val())
           }, 
           function() { 
               $('#tableHolder').hrReplace();
           }
        );
    }
});

Upvotes: 1

Related Questions