Skip to content
Related Articles

Related Articles

How to Show and Hide div elements using Checkboxes ?
  • Last Updated : 17 Sep, 2019

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                  

chevron_right


Output:



  • Before selecting the any checkbox:
  • After selecting the one checkbox:
  • After selecting the two checkbox:

Example 2: Hiding content using the checkboxes

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:

  • Before selecting the any checkbox:
  • After selecting the one checkbox:
  • After selecting the two checkbox:

Example 3: Along with alert method

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>   

chevron_right


Output:

  • Before selecting the any checkbox:
  • After selecting the checkbox:
  • After clicking the alert Ok button:
My Personal Notes arrow_drop_up
Recommended Articles
Page :