...

View Full Version : Simple function to increment a number and display it in a text box



Cpt0bvious
06-03-2005, 05:02 AM
Ok, basically I created two functions, one to increment a number, and one to decrement it. I have tried many different things, but none have worked. I already made a JavaScript keyboard and that worked fine, but it was text, not an integer. Here is what I have now, although one of my previous incarnations may have been closer to the correct one.

<Script language="JavaScript">

<!-- Function to increment the party number -->
function partyUp()
{
partyNum=document.form.party.value;

if (partyNum == "12")
{
alert("You can't have a party bigger than 12");
}
else{
document.form.party.value = partyNum + 1;
}
}

<!-- Function to decrement the party number-->
function partyDown()
{
var partyNum=document.form.party.value;

if (partyNum == "1")
{
alert("You can't have a party smaller than 1");
}
else{
document.form.party.value = partyNum - 1;
}
}

</script>
<!-- End of javascript section-->

<FORM ACTION="$thisfile" METHOD="POST">
<a href="#" onClick="partyUp()"><img src=up.jpg></a>
<a href="#" onClick="PartyDown()"><img src=down.jpg></a>

<input type=text name="party" align=top maxlength=2 size=40>

Please look this over and offer me any pointers you have. Thanks

martin_narg
06-03-2005, 09:41 AM
Is this what you are looking for mate?



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Party numbers</title>
<script>
function partyUp() {
var elParty = document.getElementById("party");
var intParty = (isNaN(elParty.value) || elParty.value.length < 1) ? 0 : elParty.value;

if( intParty++ >= 12 )
alert( "You can't have a party bigger than 12" );
else
elParty.value = intParty;

return false;
}

function partyDown() {
var elParty = document.getElementById("party");
var intParty = (isNaN(elParty.value) || elParty.value.length < 1) ? 2 : elParty.value;

if( intParty-- <= 1 )
alert( "You can't have a party smaller than 1" );
else
elParty.value = intParty;

return false;
}

</script>
<style>
body { font-family: 'Courier New', Courier, mono; font-size: 12px; color: #000000; }
a { font-family: 'Courier New', Courier, mono; font-size: 18px; color: #000000; font-weight: bold; text-decoration: none; }
</style>
</head>

<body>
<form name="frmParty">
Party counter:<br>
<a href="#" onclick="return partyDown();">-</a>&nbsp;<input type="text" name="party" size="3">&nbsp;<a href="#" onclick="return partyUp();">+</a>
</form>
</body>
</html>


Hope this helps!

m_n

Cpt0bvious
06-03-2005, 10:47 AM
Thank you very much. That is exactly what i need it to do. You are a lifesaver



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum