seoppc
seoppc

Reputation: 2824

Using jQuery functions .each() and .live()

I am using the following jQuery code for table calculations....

$(document).ready(function() {
    var totalregfee = 0;
    $('.reg_fee').each(function(){
        totalregfee+= parseFloat($(this).html());
    });
    $('.total_regfee').html(totalregfee);
});

It works perfect when the page loads, but if I go to the next page or increase the data rows on page using tablesorter.pager, total doesn't get updated. How can I use jQuery .live() on the above code?

Please ask if you need more details, thanks for support.

Upvotes: 2

Views: 2088

Answers (1)

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76890

You should make a function

var updateTotal = function(){
    var totalregfee = 0;
    $('.reg_fee').each(function(){
        totalregfee+= parseFloat($(this).html());
    });
    $('.total_regfee').html(totalregfee);
}

$(document).ready(function() {
    //call it on document ready
    updateTotal();
    //call it when you click a button
    $('#button').click(updateTotal);


});

Then call it on document ready and whenever you need to uopdate the total

To use it with tablesorter you should bind it to sortend (this works when you sort the table)

$('#idOfYourTable').bind("sortEnd", updateTotal);

Upvotes: 1

Related Questions