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
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 />

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.