add 1

I

IntLOG

Hi, how can I increment a value by one at every click?
I tried the following code but the value always remains 1:

function Foo(){
this.iCount=0;
}
Foo.prototype.addOne = function(){
this.iCount+=1;
}
instance = new Foo();



<button onClick='a=instance.addOne();alert(a);'>Click</button>

How can I reach my goal??
 
D

DoomedLung

try...

function Foo(){

this.iCount++;

}

This may be a bit clearer...

var Foo = new function(){
this.iCount = 0;
}

function alertI(){
//check the value of 'iCount' before incrementing takes place
alert(Foo.iCount);
//increment iCount by 1
Foo.iCount++;
//check that the new value of 'iCount' has incremented
alert(Foo.iCount);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top