Javascript DOM Form Convert Fahrenheit to Celsius

Description

Javascript DOM Form Convert Fahrenheit to Celsius

View in separate window

<!DOCTYPE html> 
<html lang="en"> 
<body> 
    <form action="" name="form1"> 
        <p> 
            <input type="text" name="txtCalcBox" value="0.0" /> 
        </p> 
        <input type="button" value="Convert to centigrade" 
               id="btnToCent" name="btnToCent" /> 
    </form> 
    <script> 
        function convertToCentigrade(degFahren) { 
            let degCent = 5 / 9 * (degFahren - 32); 

            return degCent; //from   w  w w  .ja  v a  2 s .  c  o  m
        } 

        function btnToCentClick() { 
            let calcBox = document.form1.txtCalcBox; 

            if (isNaN(calcBox.value) == true  || calcBox.value == "") { 
                calcBox.value = "Error Invalid Value"; 
            } else { 
                calcBox.value = convertToCentigrade(calcBox.value); 
            } 
        } 

        document.getElementById("btnToCent") 
                 .addEventListener("click", btnToCentClick); 
    </script> 
</body> 
</html> 



PreviousNext

Related