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:

Tutorials
- Java 26 (JDK 26) Features with examples
- Top 10 Reasons to Learn Java in 2026
- JDK 26 Tutorial
- Java Certification Roadmap 2026: Which Oracle Certs Matter
- Install JDK 26: A Complete Beginner's Guide (2026)
- What Is Java? A Complete, Up-to-Date Guide for 2026
- Java 26 HTTP3 tutorial - HTTP 3 support in JDK 26 - How to use HTTP/3 in Java 26 application?
- JDK 28 Preview: Value Objects, Shenandoah GC & JSON API
- Java Tutorials


