I have a dropdown list containing 7 options. Each option a user selects they have to fill out a description in a text area right below it. What I'm trying to achieve is when an option is selected show the appropriate div and hide the others. This is what I've got so far
$(document).ready(function () {
$("#define-labelling").hide();
... hide the remaining divs here
$("#ProjectStatus").change(function () {
var selectedValue = "";
$("#ProjectStatus option:selected").each(function () {
selectedValue += $(this).val();
if (selectedValue === "Define (Labelling)") {
$("#define-labelling").fadeIn("slow");
}
... More if statements here for the other divs
});
});
});
});
My questions is, is there a cleaner way to go about this? Because currently I have multiple if statements and hiding and show the appropriate divs is getting a little ugly.
Update following @Mairaj answer
function showDiv() {
var divID = $("#ProjectStatus option:selected").attr("data-div");
alert(divID); //this comes as undefined
$("#" + divID).show();
$("#" + divID).siblings().hide();
}
I've added two divs as:
<div class="form-group" id="Define (Labelling)" style="display: none;">
@Html.LabelFor(model => Model.LabellingInfo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => Model.LabellingInfo, htmlAttributes: new { @class = "form-control description" })
</div>
</div>
<div class="form-group" id="Analysis (Root Cause)" style="display:none;">
@Html.LabelFor(model => Model.RootCauseInfo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => Model.RootCauseInfo, htmlAttributes: new { @class = "form-control description" })
</div>
noneand.none{display: none;},if change happens then$(#ProjectStatus).addClass('none')and$("#define-labelling")..removeClass('none')- gdreamlend