+ 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
5 Réponses
+ 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.
+ 4
Thanks all
+ 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' ;)
+ 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>
+ 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