0
Error in JS (Noob)
I'm not a pro on this. Please somebody help me. <!DOCTYPE html> <html> <head> </head> <body> <input type="text" id="inputnumber"/> <button id="button" onclick="button()">Clique para ter o resultado da raiz quadrada</button> <script type="text/javascript"> function button(){ var num = document.getElementById("inputnumber");//Find the number for the square root var numf = Math.sqrt(num);//Find the number square root document.write("A raiz quadrada é " + numf + ".");//Shows the number square root } //It's is resulting in "The square root of the number is NaN" </script> </body> </html>
2 Réponses
+ 1
Sorry! I was wrong. I forgot that you're only getting the input field, you're not actually getting the value. So to fix that you have to put num.value to get what's actually in the textbox. So to fix the code it would look like this:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
//type="text" or type="number
<input type="number" id="inputnumber"/>
<button id="button" onclick="button()">Clique para ter o resultado da raiz quadrada</button>
<script type="text/javascript">
function button(){
var num = document.getElementById("inputnumber");//Find the number for the square root
var numf = Math.sqrt(num.value);//Find the number square root
document.write("A raiz quadrada é " + numf + ".");//Shows the number square root
}
//It's is resulting in "The square root of the number is NaN"
</script>
</body>
</html>
Alternatively, when you define the num variable, you can change that to be
var num = document.getElementById("inputnumber").value;
0
It still NaN. I try both options.