By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,689 Members | 1,915 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,689 IT Pros & Developers. It's quick & easy.

add 1

P: n/a
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??

Oct 9 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
try...

function Foo(){

this.iCount++;

}

Oct 9 '06 #2

P: n/a


On Oct 9, 10:37 am, "DoomedLung" <doomedl...@googlemail.comwrote:
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);
}

Oct 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.