Calculate with Javascript and return to html

Asked

Viewed 2,252 times

1

So guys, I just need to get some HTML information and get back the result of the calculation, but I couldn’t.

<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>
  • 0,2 swap for 0.2. Where did this "Parent" ?

1 answer

3


Three problems to be corrected:

to) In Javascript numbers with decimal part have a point and not a comma.

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

Fix the line

var myCalc = pessoas*0,2;

for

var myCalc = Number(pessoas) * 0.2;

c) To change the value of a input you have to use .valueand not .Text

jsFiddle: https://jsfiddle.net/f7yL1rah/

  • 1

    Thanks guy settled here

  • @lipesmile great! If you want you can mark the answer as accepted.

  • Unfortunately I don’t have 15 in reputation, I have 14 or else I would have scored

  • @lipesmile to mark do not need reputation, only to vote. You must click on a symbol that is grey, just below where you vote on the answer.

  • @lipesmile: http://meta.pt.stackoverflow.com/a/1079/129

Browser other questions tagged

You are not signed in. Login or sign up in order to post.