Open In App

How to Show and Hide div elements using Checkboxes ?

Improve
Improve
Like Article
Like
Save
Share
Report

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:

jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it’s philosophy of “Write less, do more”.
You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.



Last Updated : 03 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads