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:

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: JavaScript Checkbox getElementById
Post your Comment