Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,158 Points

Javascript shows no errors but no output is given

I am working on a temperature converter using toggleDisplay to change labels and produce either F or C temperatures.. I have debugged my javascript repeatedly but cannot figure out why no output occurs for the document. I am reproducing my HTML and js below. If anyone can see the error, I would be grateful.

HTML

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Convert Temperatures</title>
    <link rel="stylesheet" href="convert_temp.css">
    <script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
    <script src="convert_temp.js"></script>
</head>

<body>
    <main>
        <h1>Convert temperatures</h1> 
        <div>
            <input type="radio" name="conversion_type" id="to_celsius" checked>Fahrenheit to Celsius
        </div>
        <div>
            <input type="radio" name="conversion_type" id="to_fahrenheit">Celsius to Fahrenheit
        </div>
        <div>
            <label id="degree_label_1">Enter F degrees:</label>
            <input type="text" id="degrees_entered">
        </div>
        <div>
            <label id="degree_label_2">Degrees Celsius:</label>
            <input type="text" id="degrees_computed" disabled>
        </div>
        <div>
            <label></label>
            <input type="button" id="convert" value="Convert" />
        </div> 
    </main>
    <script src="convert_temp.js"></script>
</body>
</html>

JS

"use strict";

window.onload = function() {
console.log("SPLUNGE");
const $ = selector => document.querySelector(selector);
/*********************
*  helper functions  *
**********************/

const calculateFahrenheit = temp => temp * 9/5 + 32;
const calculateCelsius = temp => (temp-32) * 5/9;

const toggleDisplay = (label1Text, label2Text) => {
$("#degree_label_1").innerText = label1Text;
    console.log(label1Text);
$("#degree_label_2").innerText = label2Text;
    console.log(label2Text);
// move focus
    $("#degrees_entered").focus();
};

const toCelsius = () => toggleDisplay("Enter F degrees:", "Degrees Celsius:");
const toFahrenheit = () => toggleDisplay("Enter C degrees:", "Degrees Fahrenheit:");       

/****************************
*  event handler functions  *
*****************************/
const convertTemp = () => {   
    var val = parseFloat($("degrees_entered").value);
    if(isNaN(val)) {
            alert("Enter a valid number for degrees");
        } else {
            if($("to_celsius").checked) {
                $("degrees_computed").value = calculateCelsius(val);
                toCelsius;
                console.log(calculateCelsius(val));
        } else {
            $("degrees_computed").value = calculateFahrenheit(val);
            toFahrenheit;
            console.log(calculateFahrenheit(val));
        }

}
};

document.addEventListener("DOMContentLoaded", () => {
    // add event handlers
    $("#convert").addEventListener("click", convertTemp);
    $("#to_celsius").addEventListener("click", toCelsius);
    $("#to_fahrenheit").addEventListener("click", toFahrenheit);

    // move focus
    $("#degrees_entered").focus();
});
};

1 Answer

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hi Nancy Melucci 👋

I think the issue lies in how you're only loading the JavaScript after the HTML document has been completely parsed due to the window.onload at the start of your JS file.

Your JavaScript won't run until the content has been loaded, therefor the DOMContentLoaded eventListener at the bottom will not fire again, that event has already been triggered previously. Because this eventListener is not being triggered the 3 other eventListeners will not be created.

Since your script tag in the HTML is already placed at the bottom before the closing body tag I think you can do without the window.onload function and DOMContentLoaded eventListener. Removing those will make sure the click event listeners are set up correctly upon page load and your functions should start working 🙂

Hope that helps to get you going again! 😄