Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable + 1 on click : Help needed !

Status
Not open for further replies.

gafir

Technical User
Mar 22, 2007
1
US
Hello,
I am a beginner at JavaScript and need your help. I'm trying to create a function that will count the number of a variable and display different alerts according to the number.

However, everytime the function is executed, I want the variable number of the previous execution to be kept, here is the script that doesn't work. variable countcard is always 1 :-(.
Do you know what's wrong ?, thanks for your help. :

Code:

<script language="JavaScript">
function cardpick()
{
if (typeof countcard=="undefined")
{ var countcard = 0 }
var pick=(countcard + 1)
var countcard=(pick)
if (countcard > 3)
{ alert("You have picked all the cards you needed : "+countcard)
} else { alert("You have not picked all the cards yet : "+countcard)} } </script>
----------------------------------

and the html event:
HTML Code:

<a href="#" onclick="cardpick()">Click here</a>
------

Here I try to explain what i did :
function cardpick()
{
if (typeof countcard=="undefined")
{ var countcard = 0 }
// Here is : if countcard is not defined, then set it at 0, else don't change anything.
var pick=(countcard + 1)
var countcard=(pick)
// With this I set countcard + 1
if (countcard > 3)
{
alert("You have picked all the cards you needed : "+countcard)}
else {alert("You have not picked all the cards yet : "+countcard)} }

Here, on click we get the messages, however, I want that everytime we click on the link, +1 is added. So first time you click on the link, you get 'You have not picked all the cards yet : 1'

Second time you click on the link :
'You have not picked all the cards yet : 2'

Third time you click :
'You have not picked all the cards yet : 3'

fourth time and more :
'You have picked all the cards you needed : 4.etc...'

With my script, variable countcard is always 1 :-(.
Do you know what's wrong ?, thanks for your help.

Gafir
Reply With Quote
 
You need to make countcard a global variable so that it is not reset each time the function is called. Define it outside the function:
Code:
<script type="text/javascript">
[!]var countcard = 0;[/!]

function cardpick() {
   var pick=(countcard + 1)
   countcard=(pick)
   if (countcard > 3) {
      alert("You have picked all the cards you needed : "+countcard);
   }
   else {
      alert("You have not picked all the cards yet : "+countcard);
   }
}

</script>

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top