Beginning JavaScript - displaying temperature value in Inner HTML

前端 未结 3 970
迷失自我
迷失自我 2021-01-22 04:07

I am in a beginning JavaScript class and very new to programming. I am attempting to make a program that converts Celsius to Fahrenheit or Fahrenheit to Celsius depending on whi

3条回答
  •  清酒与你
    2021-01-22 04:35

    Your output fields are elements. To fill them in you must assign to .value, not .innerHTML.

    "use strict";
    var $ = function(id) {
      return document.getElementById(id);
    };
    
    var toFahrenheit = function() {
      if ($("to_fahrenheit").checked) {
        $("degree_label_1").innerHTML = "Enter C degrees:";
        $("degree_label_2").innerHTML = "Degrees Fahrenheit:";
      }
      clearTextBoxes();
    }
    var toCelsius = function() {
      if ($("to_celsius").checked) {
        $("degree_label_1").innerHTML = "Enter F degrees:";
        $("degree_label_2").innerHTML = "Degrees Celsius:";
      }
      clearTextBoxes();
    }
    var convertTemp = function() {
      var fahrenheit;
      var celsius;
    
      if ($("to_fahrenheit").checked) {
        celsius = $("degrees_entered").value;
        celsius = (celsius * (9 / 5)) + 32;
        celsius = parseFloat(celsius);
        celsius = celsius.toFixed(0);
        $("degrees_computed").value = celsius;
      }
    
      if ($("to_celsius").checked) {
        fahrenheit = $("degrees_entered").value;
        fahrenheit = parseFloat(fahrenheit);
        fahrenheit = (fahrenheit - 32) * (5 / 9);
        fahrenheit = fahrenheit.toFixed(0);
        $("degrees_computed").disabled = false;
        $("degrees_computed").value = fahrenheit;
      }
    }
    
    var clearTextBoxes = function() {
      $("degrees_entered").value = "";
      $("degrees_computed").value = "";
    };
    
    window.onload = function() {
      $("convert").onclick = convertTemp;
      $("to_celsius").onclick = toCelsius;
      $("to_fahrenheit").onclick = toFahrenheit;
      $("degrees_entered").focus();
    };
    
    
    
    
      
      Temperature Converter
      
    
    
    
      

    Temperature Converter

    Fahrenheit to Celsius
    Celsius to Fahrenheit




提交回复
热议问题