I have a loop which is supposed to go around four check boxes I have and for every one that's checked false, it should add one onto a count. However, it doesn't work. It seems to only loop around one checkbox. Can anybody help me find out why?
JQuery:
$("#CreateUserButton").on('click', function () {
var rt = false;
var count = 0;
$(".RoleCheck").each(function () {
if ($(".RoleCheck").prop('checked') == false) {
count = count + 1;
alert("Added one");
}
});
if (count == 4) {
alert("All four been detected");
$("#ModalErrorText").html("Error : A Role has not been selected.");
rt = false;
}
$("#ErrorBox").prop('hidden', rt);
return rt;
});
HTML:
<div class="col-md-10" id="createButton">
@foreach (var item in (SelectList)ViewBag.RoleId) {
<input type="checkbox" name="SelectedRoles" value="@item.Value" class="checkbox-inline RoleCheck" />
@Html.Label(item.Value, new { @class = "control-label" })
}
</div>
I know that it has something to do with the foreach loop in the HTML to create the select boxes.