3
Answers

show and hide when radio button is clicked

Photo of Garima Bansal

Garima Bansal

1y
576
1

I have a radio button 

 @Html.RadioButtonFor(model => model.flag, "RA", htmlAttributes: new { @id = "Roflag2", })
    Call for Addl. Particulars   

and a div

<div class="row" id="callfor">
                        
                    </div>

IF radio button is clicked then this div shown otherwise hide,

How can i do this?

Answers (3)

3
Photo of Ali Benchaaban
448 3.1k 120.8k 1y

Hello, 
Use this code within your ASP.NET code
 


    @Html.RadioButtonFor(model => model.flag, "RA", new { id = "Roflag2" })
    <label for="Roflag2">Call for Addl. Particulars</label>

    <div class="row" id="callfor" style="display: none;">
        This is the content inside the div.
    </div>

@section Scripts {
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

    <script>
        $(document).ready(function() {
            // Initially hide the div
            $('#callfor').hide();

            // Listen for change event on radio button
            $('#Roflag2').change(function() {
                // Check if the radio button is checked
                if ($(this).is(':checked')) {
                    // Show the div if radio button is checked
                    $('#callfor').show();
                } else {
                    // Hide the div if radio button is not checked
                    $('#callfor').hide();
                }
            });
        });
    </script>
}


Regards,
BENCHAABAN Ali

Accepted
2
Photo of Naimish Makwana
134 13.8k 203.1k 1y

You can use jQuery to achieve this. Here’s a simple example:

$(document).ready(function() {
    $('#Roflag2').change(function() {
        if(this.checked) {
            $('#callfor').show();
        } else {
            $('#callfor').hide();
        }
    });
});

In this code, we’re using the change event to detect when the radio button’s state changes. If the radio button is checked (this.checked is true), we show the div with $('#callfor').show(). If the radio button is not checked, we hide the div with $('#callfor').hide().

Please note that this code assumes that you have included the jQuery library in your project. If you haven’t, you can include it with the following line in your HTML file:

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

This line should be placed in the <head> section of your HTML file. If you’re already using a different version of jQuery, you don’t need to include this line.

Also, please ensure that the radio button and the div have the correct ids (Roflag2 and callfor respectively) as specified in the jQuery selectors ($('#Roflag2') and $('#callfor')). If the ids are different, you should replace Roflag2 and callfor with the actual ids.

This code should be placed in a <script> tag either in the <head> section (if you’re using $(document).ready()) or at the end of the <body> section of your HTML file.

Remember to test this code thoroughly to ensure it works as expected in your specific application context. Let me know if you have any questions!

Thanks

1
Photo of Garima Bansal
959 742 49.4k 1y

below is the div 

 <div class="row" id="callfor">
                        @if (Model.remarkscrunity == "Y")
                        {
                            <span style='font-size:25px; color : green;'>@Html.ActionLink("General Remarks", "remarkscrunity", "Inspection", new { @applnno = Model.applnno }, htmlAttributes: new { @class = "btn btn-orange turbolinks", style = "font-size: 18px", @HttpMethodConstraint = "Post" }) &#10004;</span>
                        }
                        else
                        {
                            @Html.ActionLink("General Remarks", "remarkscrunity", "Inspection", new { @applnno = Model.applnno }, htmlAttributes: new { @class = "btn btn-orange turbolinks", style = "font-size: 18px", @HttpMethodConstraint = "Post" })
                        }
                    </div>