javascript add/subtect for form buttons

Discussion in 'JavaScript' started by StealthRT, Mar 13, 2007.

  1. #1
    Hello all i am looking for some code that will allow me to add or subtract 1 from a value of 0.

    Hopefully that makes since. Heres the setup...

    I have 2 buttons.... ones a "+" and the others a "-". When the user hits the "+" then it adds one to the name of the item in a hidden textbox like so:

    Tomatos 1

    If i hit the "-" button it would look like this:

    Tomatos 0

    And of course i would need to check everytime if the number goes into a negitive and spit out a warning.

    I havent been able to find sample code for this nor do know what to even look for.

    Any help would be great! :)

    David
     
    StealthRT, Mar 13, 2007 IP
  2. StealthRT

    StealthRT Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Ok i found this code:
    
    onclick="document.frmSignIn.textfield222.value+='1'"
    
    Code (markup):
    And as u know it adds a "1" to the textbox. however everytime i hit it it still adds a "1"... 11111 etc...

    How can i take the default textbox value and add to it like:
    Textbox = 0

    User clicks the button twice:
    Textbox = 2

    instead of:
    Textbox = 11

    Thanks, :)
    David
     
    StealthRT, Mar 13, 2007 IP
  3. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Try this. Haven't checked.

    
    function getValue(){
        txt = document.forms[0].fieldname.value;
        arTxt = txt.split(' ');
        val = arTxt[arTxt.length - 1];
        delete(arTxt[arTxt.length - 1]);
        return [arTxt.join(' '), val]
    }
    
    Code (markup):
    Using the above code you can separate the name and value. Try it and let me know whether it works or not.

    Using this method you can manage any number of fields, provided the number is the last part of the string and is separated with a space.
     
    Aragorn, Mar 13, 2007 IP
  4. StealthRT

    StealthRT Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Aragorn:
    Thanks for the reply. I have desided to just add a textbox for the number. So can you convert your code you posted to just use a textbox now instread of the item name and value?

    Thanks!
    David
     
    StealthRT, Mar 13, 2007 IP