Doesn't work in an actual HTML form

javascript

#1



https://www.codecademy.com/courses/javascript-beginner-en-6LzGd/2/5?curriculum_id=506324b3a7dffd00020bf661


I have worked through the functions exercise but when I try pacing the code for example lesson 12 in a HTML page (outside CodeCademy) it does NOT work. Its all well and good learning things in CodeCademy but how about applying them in a HTML page. How do we do that?


Here is my entire HTML and it outputs NOTHING!!

<html>
<head>
<script type="text/javascript">

 var sleepCheck = function (numHours) {
    var sleepStatus = ""
    if (numHours >= 8) {
        sleepStatus = "You're getting plenty of sleep! Maybe even too much!";
    }
    else {
        sleepStatus = "Get some more shut eye!";
    }
    return sleepStatus;
};
</script>
</head>
<body onload="sleepCheck(10);">

</body>
</html>


#2

instead of giving the function return output you need to specify were to add it to the html. try to create a

in body with an id then set the destination to out put to with document.getElementById("id"). innerHTML = sleepstatus;


check out this link
w3schools


#3

sorry was supposed to be a paragraph tag but didn't show up here

<DOCTYPE html>
<html>
<head>
<script type="text/javascript">

 var sleepCheck = function (numHours) {
    var sleepStatus = ""
    if (numHours >= 8) {
        sleepStatus = "You're getting plenty of sleep! Maybe even too much!";
    }
    else {
        sleepStatus = "Get some more shut eye!";
    }
   document.getElementById("demo").innerHTML = sleepStatus;
};
</script>
</head>
<body onload="sleepCheck(10);">
<p id="demo"></p>
</body>
</html>

#4

@ajfarroll
apart what @cfecteau1 presented
you could instead of your
return sleepStatus;
use
alert(sleepStatus);
or if you use the HTML-Debugger you could read the console
after using
console.log(sleepStatus);


#5

Thanks to all who responded. I am beginning to doubt the use of return value from javascript in HTML. However I resolved it by placing this in the body.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.