JavaScript Checkbox getElementById

Ads
 

JavaScript Checkbox getElementById

This page discusses - JavaScript Checkbox getElementById

This page discusses - JavaScript Checkbox getElementById

JavaScript Checkbox getElementById()

        

In this section, we are going to use the method getElementById() for checkbox in JavaScript.

In the given example we are going to show an image on clicking the checkbox. This is done by using the JavaScript method getElementById() that is used to get the value of the particular element by their id. Here, we have defined an image and created a checkbox that calls the function showImage(). This function allows to display an image if you clicked the checkbox.

document.getElementById("image").style.visibility = 'visible'- This makes the image visible.

document.getElementById("image").style.visibility = 'hidden'- This makes the image invisible. 

Here is the code:

<html>
<h2>Use of getElementById()</h2>
<script>
function showImage(){
if(document.getElementById('check').checked==true){
document.getElementById("image").style.visibility = 'visible'; 
}
else if(document.getElementById('check').checked==false){
document.getElementById("image").style.visibility = 'hidden'; 
}
}
</script>
<body onload="showImage()">
<input type="checkbox" id="check" onclick="showImage()" />
Show Image
<form>
<img id="image" src="node.jpg">
<form>
</body>
</html>

Output will be displayed as:

As you click the checkbox, the image get displayed:

Download Source Code:

        

Ads

Ads