+ 4

What is wrong with it?

<!DOCTYPE html> <html> <head> <title>Cube</title> </head> <body> <style> body{ background-color:orange; color:purple; } </style> <script> var squa; function squa(){ var num=document.getElementById("number").value; alert(num*num); } </script> <form style=" border-radius:10px; background-color:black; "> <center>Enter No: </center><br> <center><input type="text" id="number"> </center> <br/> <center><input type="button" value="Square" onClick="sq

22nd Jan 2017, 4:09 PM
Sreerag V S
Sreerag V S - avatar
5 ответов
+ 5
I want to get the sqaure of the number entered in the text box by the alert(). Could you please run it and check the output.
22nd Jan 2017, 4:27 PM
Sreerag V S
Sreerag V S - avatar
+ 4
Thanks all
24th Jan 2017, 8:14 AM
Sreerag V S
Sreerag V S - avatar
+ 2
Your script is at begin of the <body> tag, so, when it is parsing and executing, all the html code after it isn't loaded, so your call to 'getElementById()' return 'undefined' and the 'value' property cannot be read... To fix it, put your script after the element with id 'number', or wrap your code into a function for the event listener 'window.onload' ;)
22nd Jan 2017, 4:34 PM
visph
visph - avatar
+ 2
With just complete the missing end, your code is working: <!DOCTYPE html> <html> <head> <title>Cube</title> </head> <body> <style> body{ background-color:orange; color:purple; } </style> <script> var squa; function squa(){ var num=document.getElementById("number").value; alert(num*num); } </script> <form style=" border-radius:10px; background-color:black; "> <center>Enter No: </center><br> <center><input type="text" id="number"> </center> <br/> <center><input type="button" value="Square" onClick="squa();"></center> </form> </body> </html>
22nd Jan 2017, 7:30 PM
visph
visph - avatar
+ 1
you can choose same identifier for function and for variables in your case var aqua and function square(). please explain you question correctly what you want to do
22nd Jan 2017, 4:24 PM
Keshave Jat
Keshave Jat - avatar