Open In App

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:

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:



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:

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:

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.


Article Tags :