getElementById
Advertisements
getElementById
The document.getElementById() method returns the element of specified id. In below example we receive input from input field by using document.getElementById() method, here document.getElementById() receive input value on the basis on input field id. In below example "number" is the id of input field.
Example
<html> <body> <head> <script type="text/javascript"> function squre() { var num=document.getElementById("number").value; alert(num*num); } </script> </head> <form> Enter No:<input type="text" id="number" name="number"/><br/> <input type="button" value="squre" onclick="squre()"/> </form> </body> </html>
Result
Code Explanation
var num=document.getElementById("number").value; In this code getElementById received value from input field which have id number.
Square of any number in javascript
Example
<html> <body> <head> <script type="text/javascript"> function squre() { var num=document.getElementById("number").value; var result=num*num; document.getElementById("answer").value=result; } </script> </head> <form> Enter No:<input type="text" id="number" name="number"/> <input type="button" value="squre" onclick="squre()"/> <input id="answer"/> </form> </body> </html>
Result
Google Advertisment