-3

I dynamically create a bunch of checkboxes like so:

@foreach (var rpt in reports)
{
    @* convert id to lowercase and no spaces *@
    var morphedRptName = @rpt.report.Replace(" ", string.Empty).ToLower();
    <input class="leftmargin8, ckbx" id="ckbx_@(morphedRptName)" type="checkbox" value="@rpt.report" />@rpt.report
}

I've got this event handler where I want to determine their state - checked or unchecked:

$(".ckbx").change(function () {
    if ($(this).checked) {
        alert('checkbox is unchecked');
        checkboxSelected = false;
        return;
    }
    alert('checkbox is checked');
    . . .

However, the condition "if ($(this).checked)" is always false, both when I check a checkbox and when I subsequently uncheck/deselect it.

So what do I need to do to determine when it is unchecked? I actually tried this first: "if (!$(this).checked)" but that just did the reverse - the condition was always true.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

1 Answers1

1

$(this) is jQuery object. Just use basic javascript this instead like following.

this.checked //instead of `$(this).checked`
Ibrahim Khan
  • 20,616
  • 7
  • 42
  • 55