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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Correctly passing a radio button value

    I'm a js noob but have managed to cobble together some code for a simple calculator. All the math works well, but changing the radio button doesn't have any effect. Basically, I have 4 set of equations (long if/then statements) - 2 are to be used when one radio button is checked, 2 if the other is checked.

    Code:
    <html><head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    
    <script language="JavaScript">
    
    function temp(form)
    {
    var r1 = form.r1.value;
    var GNW = parseFloat(form.GNW.value, 10);
    var BNW = parseFloat(form.BNW.value, 10);
    var totalfee = 0;
    var netfee = 0;
    var discount = 0;
    
    
    //if the "old" radio button is selected, use the the next two if/then formulas
    if (r1=1)
    {
    for brevity sake, I'll omit the first two if/then equations

    Code:
    //close out the "old" options
    }
    
    //use the "new" option
    else if (r1=2)	
    {
    // two more if/then statements
    }
    
    // this applies to all conditions
    
    discount = totalfee - netfee;
    
    form.totalfee.value = totalfee/4;
    form.netfee.value = netfee/4;
    form.discount.value = discount/4;
    }
    and now the radio buttons
    Code:
    <input name="r1" value=1 type="radio" checked="checked">Old (3m/3m)
    <input name="r1" value=2 type="radio" >Current (5m/5m)</p><p>
    hopefully the brevity makes the answer apparent, the full code is attached if I've left out a crucial detail.
    Attached Files Attached Files
    Last edited by indianagreg; 01-05-2011 at 08:13 PM.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    The compare operator is ==
    Code:
    if(r1==1) {
       ...
    }

  • Users who have thanked devnull69 for this post:

    indianagreg (01-05-2011)

  • #3
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    devnull69,
    Thanks for the quick reply. When I make that change, the whole script stops working (it just returns zeros).

    If I set the value of the radio button to words vs. numbers, will that make a difference?

    Code:
    <input name="r1" value="old" type="radio" checked="checked">Old (3m/3m)
    <input name="r1" value="new" type="radio" >Current (5m/5m)</p><p>
    and then use
    Code:
    if(r1 == "old) {
    ...
    }

  • #4
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    figured it out. used
    Code:
    if (form.r1[0].checked)
    instead of the r1 value stuff.


  •  

    Posting Permissions

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