How to get the result of the sum of two float values when clicking an input?

Asked

Viewed 54 times

0

<script type="text/javascript">
    function somarValores(){
        var s1 = document.getElementById("s1").value;
        var s2 = document.getElementById("s2").value;
        var s3 = parseInt(s1) + parseInt(s2);
        return s3;
    }
</script>

<fieldset>
    <legend>Cálculo do salário</legend>
    <label>Valor 1:</label>
    <input id="s1" type="text"/>
    <label>Valor 2: </label>
    <input id="s2" type="text"/>

    <label>Resultado: </label>
    <input id="resultado" onclick="somarValores()" type="text">

</fieldset>
  • you have done practically all the work, just need to show the result, put in a field, an Alert, console.log? a Function is returning to no one, instead of Return try to show the result. now you put in the question "float type", but you’re using parseInt, why not parseFloat?

1 answer

1

I believe that in this way it solves its problem, its code only had to basically have written the value of the result in the input concerning the result:

function somarValores(){
        var s1 = document.getElementById("s1").value;
        var s2 = document.getElementById("s2").value;
        var s3 = parseInt(s1) + parseInt(s2);
        document.getElementById("resultado").value = s3;
    }
<fieldset>
    <legend>Cálculo do salário</legend>
    <label>Valor 1:</label>
    <input id="s1" type="text"/><br>
    <label>Valor 2: </label>
    <input id="s2" type="text"/><br>

    <label>Resultado</label>
    <input id="resultado" onclick="somarValores()" type="text">

</fieldset>

Browser other questions tagged

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