0

I'm quite new to JavaScript and I have a problem. I want a user to input a number into a text input box and when they click a button, an alert box appears telling the Square Root of their number. The problem is, when the box appears, the message says 'NaN'? Can somebody help?

function convert(){
  var userInput = Number(document.getElementById("sqrinput"));
  alert(Math.sqrt(parseFloat(userInput)));
}

Thanks!

SLePort
  • 15,211
  • 3
  • 34
  • 44
  • 3
    `document.getElementById("sqrinput")` returns the DOM element itself, not the value of the input field. – Felix Kling Jan 24 '19 at 19:48
  • Possible duplicate of [How do I get the value of text input field using JavaScript?](https://stackoverflow.com/q/11563638/218196) – Felix Kling Jan 24 '19 at 19:48

1 Answers1

0

You want the value of the #sqrinput element:

function convert() {
  var userInput = Number(document.getElementById("sqrinput").value);
  alert(Math.sqrt(parseFloat(userInput)));
}
document.getElementById("btn").addEventListener("click", convert);
<input id="sqrinput" />
<button id="btn">Square</button>
SLePort
  • 15,211
  • 3
  • 34
  • 44