How to Show and Hide div elements using Checkboxes ?
In order to display data/content of a specific element by selecting the particular checkbox in jQuery we can use the toggle() method.
The toggle() method is used to check the visibility of selected elements to toggle between hide() and show() for the selected elements.
Syntax:
$(selector).toggle(speed, easing, callback)
Approach:
- Selector name for checkbox is same as the element which is used to display the
content. - CSS display property of each element is set to none using display: none; for hiding the element initially.
- Use .toggle() method for displaying and hiding the element for checking and unchecking the box.
Example 1: Displaying the content using checkboxes.
<!DOCTYPE html> <html> <head> <title> Show and Hide div elements using checkbox </title> <script src= <style type="text/css"> .selectt { color: #fff; padding: 30px; display: none; margin-top: 30px; width: 60%; background: green } label { margin-right: 20px; } </style> </head> <body> <center> <h1 style="color:green;"> GeeksforGeeks </h1> <h3> Show and Hide div elements using checkbox </h3> <div> <label> <input type="checkbox" name="colorCheckbox" value="C"> C</label> <label> <input type="checkbox" name="colorCheckbox" value="Cplus"> C++</label> <label> <input type="checkbox" name="colorCheckbox" value="Python"> Python</label> <label> <input type="checkbox" name="colorCheckbox" value="Java"> Java</label> </div> <div class="C selectt"> <strong>C</strong> is a procedural programming language</div> <div class="Cplus selectt"> <strong>C++</strong> is a general purpose programming language</div> <div class="Python selectt"> <strong>Python</strong> is a widely used general-purpose, high level programming language.</div> <div class="Java selectt"> <strong>Java</strong> is a most popular programming language for many years.</div> <script type="text/javascript"> $(document).ready(function() { $('input[type="checkbox"]').click(function() { var inputValue = $(this).attr("value"); $("." + inputValue).toggle(); }); }); </script> </center> </body> </html> |
Output:
- Before selecting the any checkbox:

- After selecting the one checkbox:

- After selecting the two checkbox:

Example 2: Hiding content using the checkboxes
<!DOCTYPE html> <html> <head> <title> Show and Hide div elements using checkbox </title> <script src= </script> <style type="text/css"> .selectt { color: #fff; padding: 30px; margin-top: 30px; width: 60%; background: green } label { margin-right: 20px; } </style> </head> <body> <center> <h1 style="color:green;"> GeeksforGeeks </h1> <h3> Show and Hide div elements using checkbox </h3> <div> <label> <input type="checkbox" name="colorCheckbox" value="C"> C</label> <label> <input type="checkbox" name="colorCheckbox" value="Cplus"> C++</label> <label> <input type="checkbox" name="colorCheckbox" value="Python"> Python</label> <label> <input type="checkbox" name="colorCheckbox" value="Java"> Java</label> </div> <div class="C selectt"> <strong>C</strong> is a procedural programming language</div> <div class="Cplus selectt"> <strong>C++</strong> is a general purpose programming language</div> <div class="Python selectt"> <strong>Python</strong> is a widely used general-purpose, high level programming language.</div> <div class="Java selectt"> <strong>Java</strong> is a most popular programming language for many years.</div> <script type="text/javascript"> $(document).ready(function() { $('input[type="checkbox"]').click(function() { var inputValue = $(this).attr("value"); $("." + inputValue).toggle(); }); }); </script> </center> </body> </html> |
Output:
- Before selecting the any checkbox:

- After selecting the one checkbox:

- After selecting the two checkbox:

Example 3: Along with alert method
<!DOCTYPE html> <html> <head> <title> Show and Hide div elements using checkbox </title> <script src= </script> <style type="text/css"> .selectt { color: #fff; padding: 30px; display: none; margin-top: 30px; width: 60%; background: green } label { margin-right: 20px; } </style> </head> <body> <center> <h1 style="color:green;"> GeeksforGeeks </h1> <h3> Show and Hide div elements using checkbox </h3> <div> <label> <input type="checkbox" name="colorCheckbox" value="C"> C</label> <label> <input type="checkbox" name="colorCheckbox" value="Cplus"> C++</label> <label> <input type="checkbox" name="colorCheckbox" value="Python"> Python</label> <label> <input type="checkbox" name="colorCheckbox" value="Java"> Java</label> </div> <div class="C selectt"> <strong>C</strong> is a procedural programming language</div> <div class="Cplus selectt"> <strong>C++</strong> is a general purpose programming language</div> <div class="Python selectt"> <strong>Python</strong> is a widely used general-purpose, high level programming language.</div> <div class="Java selectt"> <strong>Java</strong> is a most popular programming language for many years.</div> <script type="text/javascript"> $(document).ready(function() { $('input[type="checkbox"]').click(function() { var inputValue = $(this).attr("value"); $("." + inputValue).toggle(); alert("Checkbox " + inputValue + " is selected"); }); }); </script> </center> </body> </html> |
Output:
- Before selecting the any checkbox:

- After selecting the checkbox:

- After clicking the alert Ok button:

Recommended Posts:
- How to Show and Hide div elements using radio buttons?
- Hide or show HTML elements using visibility property in JavaScript
- Show and hide password using JavaScript
- jQuery | Hide/Show, Toggle and Fading methods with Examples
- How to hide or show one record from an ng-repeat within a table based on ng-click?
- How to hide elements in responsive layout using CSS ?
- How to get all selected checkboxes in an array using jQuery ?
- p5.js | show() Function
- AngularJS | ng-show Directive
- jQuery | Effect show() Method
- p5.js | hide() Function
- AngularJS | ng-hide Directive
- jQuery | hide() with Examples
- How to hide an element when printing a web page using CSS?
- How to hide a div when the user clicks outside of it using jQuery?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



