Temperature converter with JavaScript

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












This is one of my first simple javascript projects. Feedback is appreciated! Basically, everytime the select option is changed, the result is shown.






<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
</style>
</head>
<body>
</select>
<div id="sect">
<section>
<select onchange="onChange('from'); calculate()" id="selectFrom">
<option value="Celcius">Celcius</option>
<option value="Farenheit">Farenheit</option>
<option value="Kelvin">Kelvin</option>
</select>
<span class="toText">to</span>
<select onchange="onChange('to'); calculate()" id="selectTo">
<option value="Celcius">Celcius</option>
<option value="Farenheit">Farenheit</option>
<option value="Kelvin">Kelvin</option>
</select>
</section>
<section>
<input type="text" id="from" onchange="calculate()"> <span
id="toText"> to </span> <input type="text" id="to">
</section>
<br>

<section>
<p id="results"></p>
</section>
</div>
<script>
onChange('from');
onChange('to');
function onChange(fromOrTo)
if(fromOrTo === 'from')
document.getElementById("from").placeholder = document.getElementById("selectFrom").value;
else if(fromOrTo === 'to')
document.getElementById("to").placeholder = document.getElementById("selectTo").value;


function calculate()
var from = document.getElementById("selectFrom");
var to = document.getElementById("selectTo");
var fromValue = parseInt(document.getElementById("from").value);
var toValue = parseInt(document.getElementById("to").value);
if(from.value == "Celcius")
if(to.value == "Farenheit")
result = ((fromValue * 9)/5) + 32;

else if(to.value == "Kelvin")
result = fromValue + 273;
else
result = "Cannot calculate!";

document.getElementById("to").value = result.toFixed(2);

else if(from.value == "Farenheit")
if(to.value == "Celcius")
result = ((fromValue - 32) * 5)/9

else if(to.value == "Kelvin")
result = ((fromValue + 459.67) * 5)/9;
else
result = "Cannot calculate!";

document.getElementById("to").value = result.toFixed(2);

else if(from.value == "Kelvin")
if(to.value == "Celcius")
result = fromValue - 273;

else if(to.value == "Farenheit")
result = 1.8 * (fromValue-273) + 32;
else
result = "Cannot calculate!";

document.getElementById("to").value = result.toFixed(2);


</script>
</body>
</html>









share|improve this question



























    up vote
    3
    down vote

    favorite












    This is one of my first simple javascript projects. Feedback is appreciated! Basically, everytime the select option is changed, the result is shown.






    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
    </style>
    </head>
    <body>
    </select>
    <div id="sect">
    <section>
    <select onchange="onChange('from'); calculate()" id="selectFrom">
    <option value="Celcius">Celcius</option>
    <option value="Farenheit">Farenheit</option>
    <option value="Kelvin">Kelvin</option>
    </select>
    <span class="toText">to</span>
    <select onchange="onChange('to'); calculate()" id="selectTo">
    <option value="Celcius">Celcius</option>
    <option value="Farenheit">Farenheit</option>
    <option value="Kelvin">Kelvin</option>
    </select>
    </section>
    <section>
    <input type="text" id="from" onchange="calculate()"> <span
    id="toText"> to </span> <input type="text" id="to">
    </section>
    <br>

    <section>
    <p id="results"></p>
    </section>
    </div>
    <script>
    onChange('from');
    onChange('to');
    function onChange(fromOrTo)
    if(fromOrTo === 'from')
    document.getElementById("from").placeholder = document.getElementById("selectFrom").value;
    else if(fromOrTo === 'to')
    document.getElementById("to").placeholder = document.getElementById("selectTo").value;


    function calculate()
    var from = document.getElementById("selectFrom");
    var to = document.getElementById("selectTo");
    var fromValue = parseInt(document.getElementById("from").value);
    var toValue = parseInt(document.getElementById("to").value);
    if(from.value == "Celcius")
    if(to.value == "Farenheit")
    result = ((fromValue * 9)/5) + 32;

    else if(to.value == "Kelvin")
    result = fromValue + 273;
    else
    result = "Cannot calculate!";

    document.getElementById("to").value = result.toFixed(2);

    else if(from.value == "Farenheit")
    if(to.value == "Celcius")
    result = ((fromValue - 32) * 5)/9

    else if(to.value == "Kelvin")
    result = ((fromValue + 459.67) * 5)/9;
    else
    result = "Cannot calculate!";

    document.getElementById("to").value = result.toFixed(2);

    else if(from.value == "Kelvin")
    if(to.value == "Celcius")
    result = fromValue - 273;

    else if(to.value == "Farenheit")
    result = 1.8 * (fromValue-273) + 32;
    else
    result = "Cannot calculate!";

    document.getElementById("to").value = result.toFixed(2);


    </script>
    </body>
    </html>









    share|improve this question























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      This is one of my first simple javascript projects. Feedback is appreciated! Basically, everytime the select option is changed, the result is shown.






      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>Document</title>
      <style>
      </style>
      </head>
      <body>
      </select>
      <div id="sect">
      <section>
      <select onchange="onChange('from'); calculate()" id="selectFrom">
      <option value="Celcius">Celcius</option>
      <option value="Farenheit">Farenheit</option>
      <option value="Kelvin">Kelvin</option>
      </select>
      <span class="toText">to</span>
      <select onchange="onChange('to'); calculate()" id="selectTo">
      <option value="Celcius">Celcius</option>
      <option value="Farenheit">Farenheit</option>
      <option value="Kelvin">Kelvin</option>
      </select>
      </section>
      <section>
      <input type="text" id="from" onchange="calculate()"> <span
      id="toText"> to </span> <input type="text" id="to">
      </section>
      <br>

      <section>
      <p id="results"></p>
      </section>
      </div>
      <script>
      onChange('from');
      onChange('to');
      function onChange(fromOrTo)
      if(fromOrTo === 'from')
      document.getElementById("from").placeholder = document.getElementById("selectFrom").value;
      else if(fromOrTo === 'to')
      document.getElementById("to").placeholder = document.getElementById("selectTo").value;


      function calculate()
      var from = document.getElementById("selectFrom");
      var to = document.getElementById("selectTo");
      var fromValue = parseInt(document.getElementById("from").value);
      var toValue = parseInt(document.getElementById("to").value);
      if(from.value == "Celcius")
      if(to.value == "Farenheit")
      result = ((fromValue * 9)/5) + 32;

      else if(to.value == "Kelvin")
      result = fromValue + 273;
      else
      result = "Cannot calculate!";

      document.getElementById("to").value = result.toFixed(2);

      else if(from.value == "Farenheit")
      if(to.value == "Celcius")
      result = ((fromValue - 32) * 5)/9

      else if(to.value == "Kelvin")
      result = ((fromValue + 459.67) * 5)/9;
      else
      result = "Cannot calculate!";

      document.getElementById("to").value = result.toFixed(2);

      else if(from.value == "Kelvin")
      if(to.value == "Celcius")
      result = fromValue - 273;

      else if(to.value == "Farenheit")
      result = 1.8 * (fromValue-273) + 32;
      else
      result = "Cannot calculate!";

      document.getElementById("to").value = result.toFixed(2);


      </script>
      </body>
      </html>









      share|improve this question













      This is one of my first simple javascript projects. Feedback is appreciated! Basically, everytime the select option is changed, the result is shown.






      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>Document</title>
      <style>
      </style>
      </head>
      <body>
      </select>
      <div id="sect">
      <section>
      <select onchange="onChange('from'); calculate()" id="selectFrom">
      <option value="Celcius">Celcius</option>
      <option value="Farenheit">Farenheit</option>
      <option value="Kelvin">Kelvin</option>
      </select>
      <span class="toText">to</span>
      <select onchange="onChange('to'); calculate()" id="selectTo">
      <option value="Celcius">Celcius</option>
      <option value="Farenheit">Farenheit</option>
      <option value="Kelvin">Kelvin</option>
      </select>
      </section>
      <section>
      <input type="text" id="from" onchange="calculate()"> <span
      id="toText"> to </span> <input type="text" id="to">
      </section>
      <br>

      <section>
      <p id="results"></p>
      </section>
      </div>
      <script>
      onChange('from');
      onChange('to');
      function onChange(fromOrTo)
      if(fromOrTo === 'from')
      document.getElementById("from").placeholder = document.getElementById("selectFrom").value;
      else if(fromOrTo === 'to')
      document.getElementById("to").placeholder = document.getElementById("selectTo").value;


      function calculate()
      var from = document.getElementById("selectFrom");
      var to = document.getElementById("selectTo");
      var fromValue = parseInt(document.getElementById("from").value);
      var toValue = parseInt(document.getElementById("to").value);
      if(from.value == "Celcius")
      if(to.value == "Farenheit")
      result = ((fromValue * 9)/5) + 32;

      else if(to.value == "Kelvin")
      result = fromValue + 273;
      else
      result = "Cannot calculate!";

      document.getElementById("to").value = result.toFixed(2);

      else if(from.value == "Farenheit")
      if(to.value == "Celcius")
      result = ((fromValue - 32) * 5)/9

      else if(to.value == "Kelvin")
      result = ((fromValue + 459.67) * 5)/9;
      else
      result = "Cannot calculate!";

      document.getElementById("to").value = result.toFixed(2);

      else if(from.value == "Kelvin")
      if(to.value == "Celcius")
      result = fromValue - 273;

      else if(to.value == "Farenheit")
      result = 1.8 * (fromValue-273) + 32;
      else
      result = "Cannot calculate!";

      document.getElementById("to").value = result.toFixed(2);


      </script>
      </body>
      </html>








      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>Document</title>
      <style>
      </style>
      </head>
      <body>
      </select>
      <div id="sect">
      <section>
      <select onchange="onChange('from'); calculate()" id="selectFrom">
      <option value="Celcius">Celcius</option>
      <option value="Farenheit">Farenheit</option>
      <option value="Kelvin">Kelvin</option>
      </select>
      <span class="toText">to</span>
      <select onchange="onChange('to'); calculate()" id="selectTo">
      <option value="Celcius">Celcius</option>
      <option value="Farenheit">Farenheit</option>
      <option value="Kelvin">Kelvin</option>
      </select>
      </section>
      <section>
      <input type="text" id="from" onchange="calculate()"> <span
      id="toText"> to </span> <input type="text" id="to">
      </section>
      <br>

      <section>
      <p id="results"></p>
      </section>
      </div>
      <script>
      onChange('from');
      onChange('to');
      function onChange(fromOrTo)
      if(fromOrTo === 'from')
      document.getElementById("from").placeholder = document.getElementById("selectFrom").value;
      else if(fromOrTo === 'to')
      document.getElementById("to").placeholder = document.getElementById("selectTo").value;


      function calculate()
      var from = document.getElementById("selectFrom");
      var to = document.getElementById("selectTo");
      var fromValue = parseInt(document.getElementById("from").value);
      var toValue = parseInt(document.getElementById("to").value);
      if(from.value == "Celcius")
      if(to.value == "Farenheit")
      result = ((fromValue * 9)/5) + 32;

      else if(to.value == "Kelvin")
      result = fromValue + 273;
      else
      result = "Cannot calculate!";

      document.getElementById("to").value = result.toFixed(2);

      else if(from.value == "Farenheit")
      if(to.value == "Celcius")
      result = ((fromValue - 32) * 5)/9

      else if(to.value == "Kelvin")
      result = ((fromValue + 459.67) * 5)/9;
      else
      result = "Cannot calculate!";

      document.getElementById("to").value = result.toFixed(2);

      else if(from.value == "Kelvin")
      if(to.value == "Celcius")
      result = fromValue - 273;

      else if(to.value == "Farenheit")
      result = 1.8 * (fromValue-273) + 32;
      else
      result = "Cannot calculate!";

      document.getElementById("to").value = result.toFixed(2);


      </script>
      </body>
      </html>





      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>Document</title>
      <style>
      </style>
      </head>
      <body>
      </select>
      <div id="sect">
      <section>
      <select onchange="onChange('from'); calculate()" id="selectFrom">
      <option value="Celcius">Celcius</option>
      <option value="Farenheit">Farenheit</option>
      <option value="Kelvin">Kelvin</option>
      </select>
      <span class="toText">to</span>
      <select onchange="onChange('to'); calculate()" id="selectTo">
      <option value="Celcius">Celcius</option>
      <option value="Farenheit">Farenheit</option>
      <option value="Kelvin">Kelvin</option>
      </select>
      </section>
      <section>
      <input type="text" id="from" onchange="calculate()"> <span
      id="toText"> to </span> <input type="text" id="to">
      </section>
      <br>

      <section>
      <p id="results"></p>
      </section>
      </div>
      <script>
      onChange('from');
      onChange('to');
      function onChange(fromOrTo)
      if(fromOrTo === 'from')
      document.getElementById("from").placeholder = document.getElementById("selectFrom").value;
      else if(fromOrTo === 'to')
      document.getElementById("to").placeholder = document.getElementById("selectTo").value;


      function calculate()
      var from = document.getElementById("selectFrom");
      var to = document.getElementById("selectTo");
      var fromValue = parseInt(document.getElementById("from").value);
      var toValue = parseInt(document.getElementById("to").value);
      if(from.value == "Celcius")
      if(to.value == "Farenheit")
      result = ((fromValue * 9)/5) + 32;

      else if(to.value == "Kelvin")
      result = fromValue + 273;
      else
      result = "Cannot calculate!";

      document.getElementById("to").value = result.toFixed(2);

      else if(from.value == "Farenheit")
      if(to.value == "Celcius")
      result = ((fromValue - 32) * 5)/9

      else if(to.value == "Kelvin")
      result = ((fromValue + 459.67) * 5)/9;
      else
      result = "Cannot calculate!";

      document.getElementById("to").value = result.toFixed(2);

      else if(from.value == "Kelvin")
      if(to.value == "Celcius")
      result = fromValue - 273;

      else if(to.value == "Farenheit")
      result = 1.8 * (fromValue-273) + 32;
      else
      result = "Cannot calculate!";

      document.getElementById("to").value = result.toFixed(2);


      </script>
      </body>
      </html>








      share|improve this question












      share|improve this question




      share|improve this question








      edited Mar 2 at 15:36









      200_success

      123k14142399




      123k14142399









      asked Mar 2 at 14:05









      Adit

      183




      183




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Some minor observations:



          • It's a good idea to check your html projects with a validator of some sort (for example: https://validator.w3.org/). There are some slight mistakes in your file which can easily be caught by any validator.


          • Usually you want to keep content, presentation and behavior separated from another. This means you put scripts, CSS etc. into their own files instead of having them all in your html file.


          • From a usability perspective I think it would be nicer to have explicit control over when the conversion takes place. E.g. via a button or something similiar.






          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188679%2ftemperature-converter-with-javascript%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            Some minor observations:



            • It's a good idea to check your html projects with a validator of some sort (for example: https://validator.w3.org/). There are some slight mistakes in your file which can easily be caught by any validator.


            • Usually you want to keep content, presentation and behavior separated from another. This means you put scripts, CSS etc. into their own files instead of having them all in your html file.


            • From a usability perspective I think it would be nicer to have explicit control over when the conversion takes place. E.g. via a button or something similiar.






            share|improve this answer

























              up vote
              2
              down vote



              accepted










              Some minor observations:



              • It's a good idea to check your html projects with a validator of some sort (for example: https://validator.w3.org/). There are some slight mistakes in your file which can easily be caught by any validator.


              • Usually you want to keep content, presentation and behavior separated from another. This means you put scripts, CSS etc. into their own files instead of having them all in your html file.


              • From a usability perspective I think it would be nicer to have explicit control over when the conversion takes place. E.g. via a button or something similiar.






              share|improve this answer























                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                Some minor observations:



                • It's a good idea to check your html projects with a validator of some sort (for example: https://validator.w3.org/). There are some slight mistakes in your file which can easily be caught by any validator.


                • Usually you want to keep content, presentation and behavior separated from another. This means you put scripts, CSS etc. into their own files instead of having them all in your html file.


                • From a usability perspective I think it would be nicer to have explicit control over when the conversion takes place. E.g. via a button or something similiar.






                share|improve this answer













                Some minor observations:



                • It's a good idea to check your html projects with a validator of some sort (for example: https://validator.w3.org/). There are some slight mistakes in your file which can easily be caught by any validator.


                • Usually you want to keep content, presentation and behavior separated from another. This means you put scripts, CSS etc. into their own files instead of having them all in your html file.


                • From a usability perspective I think it would be nicer to have explicit control over when the conversion takes place. E.g. via a button or something similiar.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Mar 2 at 15:27









                yuri

                3,3862832




                3,3862832






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188679%2ftemperature-converter-with-javascript%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Greedy Best First Search implementation in Rust

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    C++11 CLH Lock Implementation