I have a checkbox on my ASP.NET MVC page that I create like:
<div>
@Html.CheckBoxFor(m => m.allUsers, new { Name = "allUsersCheck" })
@Html.LabelFor(m => m.allUsers)
</div>
I also have a listbox that I create with:
@Html.ListBoxFor(x => x.selectedUsers, Model.ListofUsers, new { style = "width:200px", size = 10, Id = "lbUsers" })
I have this following script (which I wrote to disable the listbox when my checkbox is checked. I can't understand why it doesn’t work. Can someone point out what's wrong in my code please?
This script is in the same .cshtml page and not in the .js file:
<script >
$(function () {
$('#allUsersCheck').click(function () {
$('#lbUsers').prop("disabled", $(this).prop('checked'));
});
});
</script>
Because you are using the Id selector of '#allUsersCheck'. The checkbox does not have an Id attribute unlike your list box(?).
Try the following with an Id attribute: