Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have radio button list and on selection of Yes/No ,I need to hide/show textbox.
If yes is selected, then textbox is visible and If No then hidden.
If textbox is visible then I need to make it mandatory using Jquery.

What I have tried:

If radiobtn.selected checked
Then textbox is mandatory.
Posted
Updated 10-Jul-16 20:54pm

try this

<html>
<head>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

    <script>
        function check() {
            var value = $('input[name=opt]:checked').val();
            if (value == 'Yes')
                $('#txtbox').show();
            else
                $('#txtbox').hide();

            if (value == 'Yes' && $('#txtbox').val() == '') {
                alert('Please enter the mandatory field');
                return false;
            }
            else {
                alert('submit!!');
                return true;
            }

        }

        $(document).ready(function () {
            $('input[name=opt]').change(function () {
                if (this.value == 'Yes') {
                    $('#txtbox').show();
                }
                else {
                    $('#txtbox').hide();
                }
            });
        });
    </script>


</head>
<body>

    <input type="radio" name="opt" checked value="Yes"> Yes<br>
    <input type="radio" name="opt" value="No"> No<br>
    <input type="text" id="txtbox" />
    <button onclick="check()">submit</button>


</body>
</html>
 
Share this answer
 
C#
you can use $("p").hide(); and $("p").show();

$('#element').click(function() {
   if($('#radio_button').is(':checked')) { 
     $("#texboxid").show();
   }else{
     $("#texboxid").hide();
   }
});
 
Share this answer
 
Comments
Member 10926063 10-Jul-16 7:16am    
I m doing same,but I need to make textbox mandatory/required if it is visible when form is submitted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900