How to get the result of adding two values of type float when clicking on an input?

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>
    
asked by anonymous 26.04.2018 / 13:38

1 answer

1

I believe that this solves your problem, your code only needed to basically write the value of the result in input for 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>
    
26.04.2018 / 13:51