How does one remove attributes and "gray out" a textbox based on the value of a dropdown upon both page load and dropdown selection change? I've tried following advice here and here, but it only seems to be working on a dropdown change. No errors are showing in Visual Studio or in the Chrome console.
The View has a dropdown that loads with the User's saved selection, and the text box becomes unavailable only after a dropdown selection change and change back, when the original value of the dropdown value should also originally load the text box as unavailable. In other words, the text box should be unavailable at every point where the dropdown value == 1, but the following doesn't seem to do it:
<div class="col-md-12" style="clear: left;">
<label for="preferenceId" class="req">Preference</label>
<div class="col-xs-8 no-left">
@Html.DropDownListFor(m => m.PreferenceTypeID, StaticCache.GetPreferenceTypes(), new { @class = "form-control", data_parsley_required = "true" })
</div>
<div class="col-xs-4 no-right">
<div id="customAmount">
@Html.TextBoxFor(m => m.PreferenceCustomAmount, new { @class = "form-control" })
</div>
</div>
</div>
@section Scripts{
<script>
$(function () {
if ($("#PreferenceTypeID").val() != 1) {
$("#PreferenceCustomAmount").hide();
} else {
$("#PreferenceCustomAmount").show();
}
});
$("#PreferenceTypeID").change(function () {
if ($("#PreferenceTypeID").val() == 1) {
$("#PreferenceCustomAmount").attr("disabled", "disabled");
} else {
$("#PreferenceCustomAmount").removeAttr("disabled", "disabled");
}
});
</script>
}