;

How To Show and Hide Div on CheckBox Check or Uncheck Using JQuery or Javascript


Tutorialsrack 01/07/2020 Jquery Javascript

In this article, you will learn how to show and hide div on checkbox check or uncheck using jquery or javascript. There are many ways to show or hide div on checkbox check or uncheck. 

Here are some examples to show or hide div on checkbox check or uncheck using JQuery or javascript.

Example 1: Using is() Function and :checked Selector

In this example, we used the is() function and :checked selector to show or hide the div on checkbox check or uncheck using jquery.

Here is the source code of the program to show or hide the div on checkbox check or uncheck using is() function and :checked selector in JQuery.

Example 1: Using is() Function and :checked Selector
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width" />
        <title>How To Show and Hide Div on CheckBox Check or Uncheck 
          Using JQuery or Javascript?</title>
        <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
    </head>
    <body>
        <label for="chkEmail">
            <input type="checkbox" id="chkEmail" />
            Do you have Email ID?
        </label>
        <hr />
        <div id="div_email" style="display: none;">
            Enter Email ID:
            <input type="text" id="txtEmail" />
        </div>
        <div id="div_addEmail">
            Add New Email ID
        </div>
        <script>
            $(function () {
                $("#chkEmail").click(function () {
                    if ($(this).is(":checked")) {
                        $("#div_email").show();
                        $("#div_addEmail").hide();
                    } else {
                        $("#div_email").hide();
                        $("#div_addEmail").show();
                    }
                });
            });
        </script>
    </body>
</html>

Example 2: Using toggle() Function

In this example, we used the toggle() function to show or hide the div on checkbox check or uncheck using jquery. The toggle() function is used to check the visibility of selected elements to toggle between hide() and show() for the selected elements. This toggle() function takes the three parameters as follows: speed, easing, callback.

Here is the source code of the program to show or hide the div on checkbox check or uncheck using the toggle() function in JQuery.

Example 2: Using toggle() Function
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width" />
        <title>How To Show and Hide Div on CheckBox Check or Uncheck
          Using JQuery or Javascript</title>
        <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
        <style>
            .display {
                display: none;
            }
        </style>
    </head>
    <body>
        <label for="chkEmail">
            Do you have Email?&nbsp;
            <input type="checkbox" id="chkEmail" />
            &nbsp; yes
        </label>
        <hr />
        <div id="div_Email" class="display">
            Email ID:
            <input type="text" id="txtEmail" />
        </div>

        <script>
            $(function () {
                $("#chkEmail").on("click", function () {
                    $(".display").toggle(this.checked);
                });
            });
        </script>
    </body>
</html>

Example 3: Pure Javascript Reference

In this example, we are not using any jquery function. This example reference is purely javascript based.

Here is the source code of the program to show or hide the div on checkbox check or uncheck using the Javascript.

Example 3: Pure Javascript Reference
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width" />
        <title>How To Show and Hide Div on CheckBox Check or Uncheck 
          Using JQuery or Javascript?</title>
    </head>
    <body>
        <label for="chkEmail">
            Do you have Email?&nbsp;
            <input type="checkbox" id="chkEmail" onclick="myFunction()" />
            &nbsp; yes
        </label>
        <hr />
        <div id="div_Email" style="display: none;">
            Email ID:
            <input type="text" id="txtEmail" />
        </div>

        <script>
            function myFunction() {
                var checkBox = document.getElementById("chkEmail");
                var div = document.getElementById("div_Email");
                if (checkBox.checked == true) {
                    div.style.display = "block";
                } else {
                    div.style.display = "none";
                }
            }
        </script>
    </body>
</html>

I hope this article will help you to understand how to show and hide div on checkbox check or uncheck using jquery or javascript

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags