How to count clicks with javascript?

You can count clicks within a single request (for example, how many times a button was clicked after the page loaded). You cannot count clicks across requests (after you load another page or reload the current page).

Example:

<script type="text/javascript">var clicks = 0;</script>
<input value="Click" type="button" onclick="clicks++">

UPDATE:

You can also use the following (using jQuery) to persist it using cookies as recommended by others:

onclick="$.cookie('clicks', $.cookie('clicks') + 1);"

Leave a Comment