Boby Marley
Boby Marley

Reputation: 43

How can i get user input and output the value?

Hi i want to get what the user wrote, multiply it by some number and display the result in heading.

    <!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <input type="text" id="number" name="num" value="">
    <div id = "output">
      <h2 id="OP"></h2>
    </div>
  </body>
</html>

var valResult = parseInt(document.getElementById("number").value);
var num = 5;
var results = valResult + num;
if (valResult == 10){
document.getElementById("OP").innerHTML = results;
}

Upvotes: 1

Views: 69

Answers (1)

Mamun
Mamun

Reputation: 68933

You have some unnecessary code. Do you really need:

if (valResult == 10){....

Try the following:

function calculate(input){
  var valResult = parseInt(input.value);
  var num = 5;
  var results = valResult * num;
  document.getElementById("OP").innerHTML = results;  
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <input type="text" id="number" name="num" oninput="calculate(this)">
    <div id = "output">
      <h2 id="OP"></h2>
    </div>
  </body>
</html>

Upvotes: 2

Related Questions