#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Posts
    45
    Rep Power
    14

    basic javascript counter


    I need to setup a very basic javascript counter. Basically, if you click on "+" it should increment the value of the text field by 1. If you click "-" it will subtract the value by 1. The subtraction works fine, but the addition does not add "1" - it insteads just concatinates 1 to the string. Any help would be greatly appreciated.

    <script language="javascript">
    function addone() {
    document.forms.myform.myfield.value = document.forms.myform.myfield.value + 1;
    }

    function subtractone() {
    document.forms.myform.myfield.value = document.forms.myform.myfield.value - 1;
    }
    </script>

    <form name="myform">
    <input type="text" name="myfield" value="100" size="5"><br>
    <input type="button" value=" + " onclick="addone();">&nbsp;&nbsp;
    <input type="button" value=" - " onclick="subtractone();"><br>
    </form>
  2. #2
  3. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    <html>
    <head>
    <title>blah</title>
    <script language="javascript">
    function addone(field) {
    field.value = Number(field.value) + 1;
    }

    function subtractone(field) {
    field.value = Number(field.value) - 1;
    }
    </script>
    </head>
    <body>
    <form name="myform">
    <input type="text" name="myfield" value="100" size="5"><br>
    <input type="button" value=" + " onclick="addone(myfield);">
    <input type="button" value=" - " onclick="subtractone(myfield);"><br>
    </form>
    </body>
    </html>
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Posts
    45
    Rep Power
    14
    thanks once again Adios!

IMN logo majestic logo threadwatch logo seochat tools logo