Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
Simple temperature converter (javascript, html, try-catch) not working

I'm a complete beginner with coding and I need to write a simple program with javascript and html for an exam, but I need to stick to my professor' standard (hence the specific way this code looks).

I tried to make a simple temperature converter (celsius to fahrenheit) but I don't understand why nothing happens when I click the "convert" button.

EDIT: for some reason the converter works fine here, but when I open it in a new window it doesn't work at all. Any idea why that might be?

function writeText (node, message) {
    var nodeText = document.createTextNode(message);
    node.replaceChild(nodeText, node.firstChild);
}
function convertHandler () {
    try {
        if (nodeTemperature.value =="") {
            writeText("the field is empty");
            return;
        }
        var temperature = Number(nodeTemperature.value);
        if (isNaN(temperature)) {
            writeText(nodeTemperature.value + " is not a number");
            return;
        }
        nodeResult.value = temperature * (9/5) + 32;

    } catch ( e ) {
        alert("convertHandler" + e);
    }
}
var nodeTemperature;
var nodeConvert;
var nodeResult;
var ConvertMessage;
function loadHandler () {
    try {
        nodeTemperature = document.getElementById("temperature");
        nodeConvert = document.getElementById("convert");
        nodeResult = document.getElementById("result");
        nodeConvertMessage = document.getElementById("convertMessage");
        nodeTemperature.value = "";
        nodeResult.value = "";
        nodeConvert.onclick = convertHandler;
        var nodeText = document.createTextNode("");
        nodeConvertMessage.appendChild(nodeText);
    } catch ( e ) {
        alert("loadHandler" + e);
    }
}
window.onload = loadHandler;
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script scr="p3.js"></script>
        <title>Temperature converter</title>
    </head>
    <body>
        <h1>Convert Celsius in Fahrenheit</h1>
        <input type="text"
        id="temperature"/> Celsius
        <br>
        <input type ="button"
        id="convert"
        value="Convert"/>
        <span id="convertMessage"></span>
        <br>
        <input type="text"
        id="result"
        readonly="readonly"/>Fahrenheit
    </body>
</html>

If someone could help me that would save my (academic) life, thank you.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!