Using document.getElementById for textbox inputs

1.3k views Asked by At

I wanted the equation to take user input and give a response based on the output. The equation works just fine, I can't get it to print anything. For example the answer is 10 and it prints 10 along with a comment. Maybe its something I'm looking over, or my code could be horribly wrong. Help?

<html>
<body>

    Total=3.21+9.63 x S //equation
    <form id="function" onsubmit="return false" oninput="o.value = 3.21 + (9.63    *parseInt(c.value))">
        =3.21+(9.63 *
        <input name="c" type="number" step="any">) =
        <output name="o"> </output>

        <input type="button" value="Predict" name="myButton" onClick="myMath()"> </form>

    <div id="mys1"></div>

    <script language="JavaScript">
        //my concept
        function myMath() {
            if (((document.function.o.output) < -30))
                document.getElementById("mys1").innerHTML = 'That'
            s a little cold!'; 

        }
    </script>
</body>
</html>
2

There are 2 answers

0
erikvold On

This bit is invalid javascript:

document.getElementById("mys1").innerHTML = 'That's a little cold!'

It should look like this:

document.getElementById("mys1").innerHTML = "That's a little cold!"

To avoid the missing ' which should be causing an error in the web console.

0
Quinten Van Geersom On

Try this:

<html>
<body>

Total=3.21+9.63 x S //equation
<form id="function" onsubmit="return false" oninput="o.value = 3.21 + (9.63    *parseInt(c.value))">
    =3.21+(9.63 *
    <input name="c" type="number" step="any">) =
    <output id="o"> </output>

    <input type="button" value="Predict" name="myButton" onClick="myMath()" </form>

<div id="mys1"></div>

<script language="JavaScript">
    //my concept
    function myMath() {
        var x = document.getElementById("o").value;
        if (x < -30){
            document.getElementById("mys1").innerHTML = "That's a little cold!"; 
        }
    };
</script>
</body>
</html>

I took the value out of the output element and compared it to -30. You should use curly brackets after 'if' statements. Also your string : 'That's a little cold!' is already ended after 'That'.