In JS forms, using the <input> tag in html, why can’t I link a variable in the <script> directly to the input?

Asked

Viewed 44 times

0

<h1>Somando Valores</h1>
    <input type="number" name='txtn1' id="txtn1"> +
    <input type="number" name="txtn2" id="txtn2">
    <input type="button" value="Somar" onclick="somar()">
    <div id="res"> Resultado</div>

function somar() {



    var n1 = Number.parseFloat(window.document.getElementById('txtn1'))
    var n2 = Number.parseFloat(window.document.getElementById('txtn2'))
    var res = window.document.getElementById('res')
    var s = n1 + n2
    res.innerHTML = `A soma entre ${n1} e ${n2} é igual a ${s}`

      }

    </script>

Why can’t I do that? In this case you would have to open a variable to the cont

  • Who cont? Your question is not clear!

  • Why can’t I do that? In this case you would have to open a variable for the contents of <imput> and then open another variable by placing it inside, like this:

  • Function somar() { var txt1= window.document.getElementById('txtn1') var txt2= window.document.getElementById('txtn2') var res = window.document.getElementById('res') var n1 = Number(txt1.value) var N2 = Number(txt2.) var s = n1 + N2 res.innerHTML = A soma entre ${n1} e ${n2} é igual a ${s}

  • Edit your question in more detail to give you a more direct answer, so you can’t understand what you want.

No answers

Browser other questions tagged

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