Click here to Skip to main content
15,905,782 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<!DOCTYPE html>
<html>
<head>
<body>

<h1> Celcius to Fahrenhiet</h1>

<p>Insert a number into one of the input fields below:</p>

<p><input id="c" onchange="convert('C')"  onkeyup="convert('C')" type="number" > degrees Celsius</p>

<p><input id="f" onkeyup="convert('F')" type="number"> degrees Fahrenheit</p> 


<script>


function convert(degree)
 {
    var x;
    if (degree == "C") {
                          x = document.getElementById("c").value * 9 / 5 + 32;
                              document.getElementById("f").value = Math.round(x);
    }
   else if(degree == 'F'){

                      x = (document.getElementById("f").value -32) * 5 / 9;
                          document.getElementById("c").value = Math.round(x);
    }
}

</script>

</body>
</html>


What I have tried:

i have tried $ sign and many other attributes to the function field but it goes wrong
Posted
Updated 2-Apr-17 22:36pm

document.getElementById("c") yo can replace this by $("#c")
if you use getElementById use a dot instead like: $(".c")


var x;
   if (degree == "C") {
                         x = $("#c").val() * 9 / 5 + 32;
                             $("#f").val() = Math.round(x);
   }
  else if(degree == 'F'){

                     x = ($("#f").val() -32) * 5 / 9;
                         $("#c").val() = Math.round(x);
   }


I also should use this instead of id, makes the code more readable.

<p><input id="c" onchange="convert(this)"  onkeyup="convert(this)" type="number" > degrees Celsius</p>

<p><input id="f" onkeyup="convert(this)" type="number"> degrees Fahrenheit</p> 
function convert(e)
{
 x = $(e).val() * 9 / 5 + 32;
     $(e).val() = Math.round(x);
}
 
Share this answer
 
v8
<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
    <script>

        $(function () {
            $('#C,#F').on('keyup', function () {
                convert(this.id);
            });
            $('#C').on('change', function () {
                convert(this.id);
            });
        }); 
        function convert(degree) {
            var x;
            if (degree == "C") {
                x = parseFloat($('#C').val()) * 9 / 5 + 32;
                $('#F').val(Math.round(x));
            }
            else if (degree == 'F') {
                x = (parseFloat($('#F').val()) - 32) * 5 / 9;
                $('#C').val(Math.round(x));
            } 
        }

    </script>
</head>
<body>

    <h1> Celcius to Fahrenhiet</h1>

    <p>Insert a number into one of the input fields below:</p>

    <p><input id="C"  type="number"> degrees Celsius</p>

    <p><input id="F"  type="number"> degrees Fahrenheit</p>




</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900