How to count button clicks in Javascript

To count button clicks in Javascript you can use the following snippet.

Sample Javascript

$('myButton')
    .data('counter', 0)
    .click(function() {
        var counter = $(this).data('counter');
        $(this).data('counter', counter + 1);
        alert($(this).data('counter'));
    });

Please Share us:
Please rate this snippet

2 thoughts on “How to count button clicks in Javascript

Leave a Reply