pav
8/18/2013 - 2:07 PM

process_selected_radio_value

<html>

    <head>
    </head>

    <body>
    
        <form name="tempForm">
            <label for="temp">Temperature:</label>
            <input type="text" id="temp"><br>

            <input type="radio" name="choice" value="fahrenheit" checked />Convert to Fahrenheit <br>
            <input type="radio" name="choice" value="celsius">Convert to Celsius  <br>

            <label for="resultField">Result: </label>
            <input type="text" id="resultField"><br>

            <input type="button" value="Convert" onclick="processForm()">
        </form>

    </body>

</html>
function processForm() {
    
    var temperature = Number(document.tempForm.temp.value);
    var tempType;
    var result;

    for (var i=0; i < document.tempForm.choice.length; i++) {
   
        if (document.tempForm.choice[i].checked) {
            tempType = document.tempForm.choice[i].value;
        }
    }

    /*conditional to perform calculations based on choice */

    if (tempType === 'fahrenheit') {
        result = temperature * 9/5 + 32;
    }
    else {
      result = (temperature -  32)  *  5/9;
    }

  document.tempForm.resultField.value=result;
}