Jump to content

[SOLVED] button add value to var


justinede

Recommended Posts

Hey Guys,

 

I am trying to make a script where there is a button and everytime it is pressed, 1 gets added to my variable. I have all of that working, but i want a alert to appear when the value of my variable reaches a certain number.

 

Here is what ihave so far..

 

<script language="javascript">
				var num=0
				if (num<6)	{
					window.alert("Please add more to Num!");
				}
				else	{
					window.alert("congrats");
				}
			//-->
		</script>
            <br />
            <input name="Add" type="button" value="Add More!" onclick="num=num+1" />
            <br />
            <a href="#" onclick="alert(num)">Check Value of Num</a><br /><br />

 

Thanks,

 

Justin

Link to comment
https://forums.phpfreaks.com/topic/176849-solved-button-add-value-to-var/
Share on other sites

It is not goop practice to put logic into your trigger (i.e. onclick="num=num+1"), instead you should call a function.

 

<script type="text/javascript">

var num=0

function addToNum()
{
    num++;

    if (num<6)
    {
        window.alert("Please add more to Num!");
    }
    else
    {
        window.alert("congrats");
    }
    return;
}
</script>

<input name="Add" type="button" value="Add More!" onclick="addToNum();" />
<br />
<a href="#" onclick="alert(num)">Check Value of Num</a><br /><br />

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.