1

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;
}

1 Answer 1

2

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.