62009030
62009030

Reputation: 347

DataTable pagination only can count checkboxes of first page

I have a table, with pagination. I created a script to count the check boxes checked on the table. It works well on first page, but when i select second page, it doesn't count when I select.

Here is my script:

<script>
    $(document).ready(function(){
    var data = {
        '300x250': 0,
        '160x600': 0,
        '728x90': 0,
        '300x600': 0,
        '300x300': 0,
        '120x600': 0,
        '100x72': 0,
        '970x250': 0,
        '750x200': 0,
        '120x60': 0,
        '200x600': 0,
    }

    function registerEvents() {
        $(".checkbox").on("change", function() {
            var value = $(this).is(":checked") ? 1 : -1;
            var key = $(this).attr('value').split('_')[0];
            console.log(key, value);
            data[key] += value;
            printData();
        });
    }

    function printData() {
        $("#result").html(JSON.stringify(data, " ", 4));
        $("#result300x250").html(JSON.stringify(data['300x250'], " ", 4));

    }

    registerEvents();
    });
</script>

Hope I explained well, thanks

Upvotes: 0

Views: 408

Answers (1)

annoyingmouse
annoyingmouse

Reputation: 5699

Are those checkboxes in the DOM on page load?

Perhaps your selector should be along the lines of:

$("#tableID").on("change", ".checkbox", function(){
    /*Your function here*/
});

Upvotes: 1

Related Questions