dmanexe
dmanexe

Reputation: 1044

Radio buttons change variable in JavaScript

I have two fields which calculate a total, but I have a radio button pair that determines a variable within the equation. Here's the radio button code:

<input type="radio" name="estimate_pool_shape" value="1" /> Angle
<input type="radio" name="estimate_pool_shape" value=".86392" /> Freeform

Immediately after that, this JavaScript is then calculating the surface area of the pool based of these fields and script:

    (Length <input type="text" size="5" name="estimate_pool_length" 
onBlur="conv2inches(document.forms.mainform);" />) x 
    (Width <input type="text" name="estimate_pool_width" size="5" 
onBlur="conv2inches(document.forms.mainform);" />)

The following JavaScript already works to update the fields, I just need the variable it uses to set the field values to change based on the radio buttons.

        function conv2inches(mainform) {            

        var oin;    
        var oinches;

        if(isNaN(mainform.estimate_pool_width.value)||isNaN(mainform.estimate_pool_length.value)){  
            alert("Please enter numbers only"); 
            return false;   
        }

        oin = ((mainform.estimate_pool_width.value) + (mainform.estimate_pool_length.value) * %%%RADIO BUTTON VARIABLE HERE%%%);    
        oinches = (oin);    

        if(oinches==0){ 
            alert("Please enter valid values into the boxes");  
        }   
            mainform.estimate_pool_sqft.value = oinches;    
            return false;
        }

Upvotes: 0

Views: 2032

Answers (1)

Tim
Tim

Reputation: 2419

var estimate_pool_shape = 0; // Or some default value.  Maybe 1?
for(var i = 0; i < mainform.estimate_pool_shape.length;i++){
 if(mainform.estimate_pool_shape[i].checked){
   estimate_pool_shape = mainform.estimate_pool_shape[i].value;
   break;
 }
}

Should give you what you need.

Upvotes: 1

Related Questions