why this code is not showing correct result ,however i change the value of the text box initial the result will still be 32 as if y=0

Viewed 22

let y = document.getElementById("weathMeas").innerHTML;

function calc(y) {
  var z;
  z = (y * 1.8) + 32;
  return z;
}
document.getElementById("demo").innerHTML = calc(y);
<head>
  <section id="calcBody"> <span id="infomsg">Please enter current temperature :</span>
    <p>
      <input type="text" id="weathMeas" name="weather" value="58">
    </p>
    <input type="button" id="result" value="result!">
    <p id="demo"></p>
  </section>
</head>

1 Answers

Is this what you're looking for?

function calc() {
  let y = document.getElementById("weathMeas").value;
  let yInt = parseInt(y);
  
  var z;
  z = (yInt * 1.8) + 32;
  document.getElementById("demo").innerHTML = z;
}
<head>
  <section id="calcBody"> <span id="infomsg">Please enter current temperature :</span>
    <p>
      <input type="text" id="weathMeas" name="weather" value="58">
    </p>
    <input type="button" id="result" value="result!" onclick="calc()">
    <p id="demo"></p>
  </section>
</head>

Related