getElementById

Advertisements

Prev Tutorial Next Tutorial

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

Enter No:

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

Enter No:

Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements