user3310635
user3310635

Reputation: 53

Return value did not show up in html

This is HTML codes:

<html>

<head>
<script src = "tempScript.js" text = "text/javascript"> 
</script>
</head>

<body>
    <h3><b>Farenheit to Celsius Converter</b></h3>
    <p>Enter a temperature in degrees F:&nbsp;
        <input type="text" id="textbox" /></p>
    <p>Click this button to calculate the temperature in degrees C:&nbsp;
        <input type="submit" value="Calculate" onclick="temp ()" /></p>
    <p>Temperature in degrees C is:&nbsp;<input type="temp ()"/></p>
</body>

</html>

And this is javascript code:

function temp ()

{
    var f = document.getElementById("textbox").value;

    var c = ((f - 32.0)*5.0) / 9.0;

    return c;
}

Were there any problem with this codes?

Actually, what I wanted to do was to get the return value and then display the value in the field of "Temperature in degrees C is". But I could not find the mistakes.Please point it out if I have mistakes.

Upvotes: 0

Views: 74

Answers (4)

MasterDev
MasterDev

Reputation: 79

You cant call/assign method in 'value' below is the correct method

    <h3><b>Farenheit to Celsius Converter</b></h3>
<p>Enter a temperature in degrees F:&nbsp;
    <input type="text" id="textbox" /></p>
<p>Click this button to calculate the temperature in degrees C:&nbsp;
    <input type="submit" value="Calculate" onclick="temp();" /></p>
<p>Temperature in degrees C is:&nbsp;<input id="degrees" /></p>

  <script type="text/javascript">
    function temp() {
        var f = document.getElementById("textbox").value;

        var c = ((f - 32.0) * 5.0) / 9.0;

        document.getElementById("degrees").value = c;
    }
</script>

Upvotes: 1

halkujabra
halkujabra

Reputation: 2942

Instead of putting it in type() of input, put it as value. Working fiddle-

http://jsfiddle.net/RDue8/2/

Upvotes: 0

bansi
bansi

Reputation: 57002

<input type="temp ()"/> is not valid html. try this.

<html>

<head>
<script src = "tempScript.js" text = "text/javascript"> 
</script>
</head>

<body>
    <h3><b>Farenheit to Celsius Converter</b></h3>
    <p>Enter a temperature in degrees F:&nbsp;
        <input type="text" id="textbox" /></p>
    <p>Click this button to calculate the temperature in degrees C:&nbsp;
        <input type="submit" value="Calculate" onclick="temp ()" /></p>
    <p>Temperature in degrees C is:&nbsp;<input id="tmpDisplay" value=""/></p>
</body>

</html>
<script>
    function temp ()

{
    var f = document.getElementById("textbox").value;

    var c = ((f - 32.0)*5.0) / 9.0;
    document.getElementById("tmpDisplay").value=c;
    return false;
}
</script>

Note: I added and id for the input where the result is to be displayed. You set the value of that input to the calculated value in your javascript. Note the return false in the script. This is to prevent the form from being submitted to the server.

Upvotes: 0

TomFirth
TomFirth

Reputation: 2364

I think the error is in line:

<input type="temp ()"/>

The type of the input the format it's shown in. You need to add a value to return to:

<input type="text" value="temp()"/>

This should then display the result in html.

Upvotes: 0

Related Questions