Forum Moderators: open

Message Too Old, No Replies

how increment the value on each click?

         

PHPycho

5:21 am on Apr 18, 2007 (gmt 0)

10+ Year Member



Hello forums!
How to increment the value on each function call?
Suppose i had a function called
addRow()

//js code
addRow(row)
{
var i = 0;
alert(i);
}

suppose this function is called with onclick event of button.
when clicked first time it should alert 0, 2nd time => 2 3rd time => 3..
I hope you got my question, awaiting for your valueable help.
Thanks in advance to all of you

daveVk

5:56 am on Apr 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to use a global variable as you want count to be remembered between alerts say

var rowCount = 1;

declare this outside of any function, say top of javascript, dont call it "i" as that likely to confusing with other uses of "i" as a local variable. Set to 0 or 1 as need be.

code becomes

//js code
addRow(row)
{
alert(rowCount++);
}