SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Adding in form

  1. #1
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Adding in form

    Is there a way to add 2 fields and put the total in another field without submitting the form in javascript? So say the second they click in the total cost field or perhaps the second they add anything in either of the 2 fields being summed up it puts the total in the total field

    Thanks
    Silly

  2. #2
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure that is possible try this on 4 size!

    Code:
    <script>
    function addMyValue(val_1,val_2){
    var myValue_1=parseInt(val_1,10);
    var myValue_2=parseInt(val_2,10);
    myValue_1=myValue_1+myValue_2;
    document.theFrm.ans.value=myValue_1;
    }
    </script>
    
    <form name="theFrm">
    <input type="text" name="val1">+
    <input type="text" name="val2">=
    <input type="text" name="ans" onfocus="addMyValue(this.form.val1.value,this.form.val2.value)">
    </form>
    PS: I just love your avatar!! LOL!
    Last edited by Poop_Shoot; Aug 30, 2003 at 16:08.

  3. #3
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Poop_Shoot
    Sure that is possible try this on 4 size!
    Code:
    <script>
    function addMyValue(val_1,val_2){
    var myValue_1=parseInt(val_1,10);
    var myValue_2=parseInt(val_2,10);
    myValue_1=myValue_1+myValue_2;
    document.theFrm.ans.value=myValue_1;
    }
    </script>
    
    <form name="theFrm">
    <input type="text" name="val1">+
    <input type="text" name="val2">=
    <input type="text" name="ans" onfocus="addMyValue(this.form.val1.value,this.form.val2.value)">
    </form>
    PS: I just loveimages/smilies/heart.gif your avatar!! LOL!
    Thanks! Hmm. Question how do you add numbers with decimals?

    Edit: NM! I need to use parsefloat! Duh!!

    Silly

  4. #4
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well you could use number!

    Code:
    <script>
    function addMyValue(val_1,val_2){
    var d=document.theFrm;
    var x=d.oper.options[d.oper.selectedIndex].value;
    var val1=Number(val_1);
    var val2=Number(val_2);
    d.ans.value=eval(val1+x+val2);
    }
    </script>
    
    <form name="theFrm">
    <input type="text" name="val1" onblur="this.form.ans.value=this.value;"> 
    <select name="oper">
    <option value="*">*
    <option value="-">-
    <option value="+">+
    <option value="%">%
    <option value="/">/
    </select>
    <input type="text" name="val2" onblur="addMyValue(this.form.val1.value,this.form.val2.value)"> =
    <input type="text" name="ans" onfocus="addMyValue(this.form.val1.value,this.form.val2.value)">
    </form>


Bookmarks

Posting Permissions

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