Calculate with Javascript and return in html

1

So people, I just need to get an HTML information and return the result of the calculation, but I could not.

<html>
    <head>
        <title>Calculadora</title>
        <script language="javascript" type="text/javascript">
        var calculadora = function (){
            var pessoas = form1.pessoas.value;
            var myCalc = pessoas*0,2;
            parent.document.getElementById("calc").Text = myCalc;
        }
        </script>
        </head>
    <body>
    <title>Calculadora</title>
    Pessoas
    <form name="form1">
        <input type="Text" name="pessoas"><br>
    <input type="submit" value="calcular" onclick="return calculadora()">
    </form>
Resposta:
        <input type="Text" id="calc"  readonly ><br>
</body>
</html>
    
asked by anonymous 14.07.2016 / 22:20

1 answer

2

Three issues to fix:

a) In JavaScript numbers with decimal part have a period, not a comma.

b) input values are strings, text. You must convert this value to a number, for example Number() or parseFloat() .

Correct the line

var myCalc = pessoas*0,2;

for

var myCalc = Number(pessoas) * 0.2;

c) To change the value of a input you must use .value and not .Text

jsFiddle: link

    
14.07.2016 / 22:22