A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Javascript - Number addition in textfield

  1. #1
    Flash Developer on a break
    Join Date
    Jun 2001
    Location
    The Netherlands
    Posts
    1,370

    Javascript - Number addition in textfield

    This is the code:

    PHP Code:
    <html>
    <
    head>
    <
    script>
        function 
    stat_min(thevalue) {
            eval(
    "document."+thevalue+".value") --;
        }
        function 
    stat_plus(thevalue) {
            eval(
    "document."+thevalue+".value") ++;
        }
    </
    script>
    </
    head>
    <
    body >

        <
    INPUT class='input' TYPE='TEXT' NAME='str' SIZE=5 MAXLENGTH=

    VALUE
    =2><input class="input" type="button" value=" + " 

    onclick="stat_plus('str')"

    </
    body>
    </
    html
    It doesn't seem to work.. It's probably a simple solution. Can anyone help me?

    Already thanks,

    Jeroen

    | "Keep Flashing!"-Jeroen
    PHP multiplayer? Check this thread: PHPgateway

  2. #2
    Senior Member
    Join Date
    Apr 2001
    Posts
    296
    Try this:

    PHP Code:

    function incrementTxtField(nameOfTxtField)
    {
      var 
    value;
      
    value parseInt(document[nameOfTxtField].value);
      if(
    value == 'NaN')
      {
        
    document[nameOfTxtField].value 0;
      } else {
        
    document[nameOfTxtField].value = ++value;
      }

    parseInt attempts to convert a string to number and returns 'NaN' if it fails. The value of the text field will always be a string so it is necessary to do this.

    I'm sure you can work out how to make the decrement function!

  3. #3
    Flash Developer on a break
    Join Date
    Jun 2001
    Location
    The Netherlands
    Posts
    1,370
    Your code didn't seam to work..

    But I changed my own into this after some trying.. =P
    PHP Code:
    <script language="JavaScript"><!--
        function 
    stat_min(f,stat) {
            eval(
    "f."+stat+".value--");
        }
        function 
    stat_plus(f,stat) {
            eval(
    "f."+stat+".value++");
        }
    // --></script> 
    Kinda forgot to put the eval() around everything.

    Btw - do you know a way of making a textfield NOT editable by the user, only by javascript?

    Tnx

    | "Keep Flashing!"-Jeroen
    PHP multiplayer? Check this thread: PHPgateway

  4. #4
    Flash Developer on a break
    Join Date
    Jun 2001
    Location
    The Netherlands
    Posts
    1,370
    Nevermind.. I saved the number in the textfield to a JS var and made a function that is called onChange of the txt-field.

    | "Keep Flashing!"-Jeroen
    PHP multiplayer? Check this thread: PHPgateway

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center