;

How To Show or Hide Div on Radio Button Select Using JQuery or Javascript


Tutorialsrack 03/07/2020 Jquery Javascript

In this article, you will learn how to show and hide div on radio button selection using jquery or javascript. There are many ways to show or hide div on radio button selections. And we used the show() and hide() function to display the div on radio button selection. 

The show() function is used to show the syntax or the element of HTML that you want the user to see.  And the hide() function is used to show the syntax or the element of HTML that you want the user to see.

Here are some examples to show or hide div on radio button selections 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 radio button selections using jquery. And show() and hide() function used to show or hide the div on radio button selection.

Here is the source code of the program to show or hide the div on radio button selections 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 Radio Button Selections
          Using JQuery or Javascript?</title>
        <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
    </head>
    <body>
        <label for="chkEmail_yes">
            Do you have Email ID?
            <input type="radio" id="radio_Email_yes" value="yes" name="radio" />
            Yes &nbsp;
            <input type="radio" id="radio_Email_no" value="no" name="radio" />
            No
        </label>
        <hr />
        <div id="div_email" style="display: none;">
            Enter Email ID:
            <input type="text" id="txtEmail" />
        </div>

        <script>
            $(function () {
                $("input[name='radio']").click(function () {
                    if ($("#radio_Email_yes").is(":checked")) {
                        $("#div_email").show();
                    } else {
                        $("#div_email").hide();
                    }
                });
            });
        </script>
    </body>
</html>

Example 2: 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 radio button selections using the Javascript.

Example 2: 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>
            Do you have Email ID?
            <input type="radio" id="radio_Email_yes" value="yes" name="radio" onclick="javascript:myFunction();" />
            Yes &nbsp;
            <input type="radio" id="radio_Email_no" value="no" name="radio" onclick="javascript:myFunction();" />
            No
        </label>
        <hr />
        <div id="div_email" style="display: none;">
            Enter Email ID:
            <input type="text" id="txtEmail" />
        </div>
 
        <script>
            function myFunction() {
                var yes = document.getElementById("radio_Email_yes");
                var div_email = document.getElementById("div_email");
                if (yes.checked === true) {
                    div_email.style.display = "block";
                } else {
                    div_email.style.display = "none";
                }
            }
        </script>
    </body>
</html>

I hope this article will help you to understand how to show or hide div on radio button selections 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