Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple function to increment a number and display it in a text box

    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

  • #2
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Is this what you are looking for mate?

    Code:
    <!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
    Last edited by martin_narg; 06-03-2005 at 10:44 AM.
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #3
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much. That is exactly what i need it to do. You are a lifesaver


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •