Tuesday, June 16, 2009

How to count number of times the user clicks on a button using javascript?

Sometimes we may need to count the number of times the user clicks on a button or any other control/element in a webpage.

But it seems there no straight forward way such as static variable to do this.

Below sample javascript code can be used to achieve this.

function addCategory()
if ( typeof addCategory.no == 'undefined' )
addCategory.no =1;
addCategory.no =addCategory.no +1;

<input type=button onclick=”addCategory()”>

addCategory.no----->contains total number of times the addCategory() get invoked.
no----> is variable name. Here it acts as member variable for addCategroy() function.
addCategory---->is function name. Javascript treats every function as object. That’s why we are accessing variable as addCategory.no

More Articles...

1 comment:

Unknown said...

Hello if you want to count number clicks on a button so you can use function count();
for example click here web analysis indore

Search This Blog