Skip to content Skip to sidebar Skip to footer

Asp.net Mvc Show/hide Div When Dropdownbox Option Change

I have my Dropdownbox list in .cshtml as follow:
@Html.Label('Role', new { @class = 'col-md-2 control-label' })

Solution 1:

change both #StudentSection, #LecturerSection as .StudentSection, .LecturerSection as they seem to be classes not id

see this working on the demo

<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="form-group"><divclass="col-md-10"><selectid="Name"><option>Student</option><option>Lecturer</option></select></div></div><divclass="StudentSection"style="display:none;">
StudentSection Some contents
</div><divclass="LecturerSection"style="display:none;">
LecturerSection Some contents
</div><scripttype="text/javascript">
$(document).ready(function () {
    $(".StudentSection").hide();
    $(".LecturerSection").hide();

    $("#Name").change(function () {
        if ($("#Name").val() == "Student") {
            $(".StudentSection").show();
            $(".LecturerSection").hide();
        }
        else {
            $(".LecturerSection").show();
            $(".StudentSection").hide();
        }
    });
});
</script>

Post a Comment for "Asp.net Mvc Show/hide Div When Dropdownbox Option Change"