justinede Posted October 7, 2009 Share Posted October 7, 2009 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 More sharing options...
eatfishy Posted October 7, 2009 Share Posted October 7, 2009 It looks like your code below does what you described already, but I could be reading it wrong. Link to comment https://forums.phpfreaks.com/topic/176849-solved-button-add-value-to-var/#findComment-932611 Share on other sites More sharing options...
Psycho Posted October 8, 2009 Share Posted October 8, 2009 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 https://forums.phpfreaks.com/topic/176849-solved-button-add-value-to-var/#findComment-933064 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.