0

I want to make a table where row hide\show depending on if checkbox in the header is checked or not. My script:

<script contextmenu="text/javascript">
$(document).ready(function () {
    $("#ShowPersonalDataList").change(function () {
        if($(this).is("checked")){
            $(".PersonalDataInset").Show;
            return
        } else
            $(".PersonalDataInset").hide;
    });

});

My HTML :

<div id="CheckBoxTables">
    <table class="CBTable">
        <tr>
            <th>
                @Html.LabelFor(m => m.ColumnsNeeded.PersonalDataPartBool)
                @Html.CheckBoxFor(m => m.ColumnsNeeded.PersonalDataPartBool, new { id = "ShowPersonalDataList" })
            </th>
        </tr>
        <tr>
            <td class="PersonalDataInset">
                @Html.LabelFor(m => m.ColumnsNeeded.FirstNameBool)
                @Html.CheckBoxFor(m => m.ColumnsNeeded.FirstNameBool)
            </td>
        </tr>
        <tr>
            <td class="PersonalDataInset">
                @Html.LabelFor(m => m.ColumnsNeeded.LastNameBool)
                @Html.CheckBoxFor(m => m.ColumnsNeeded.LastNameBool)
            </td>
        </tr>
        <tr>
            <td class="PersonalDataInset">
                @Html.LabelFor(m => m.ColumnsNeeded.AppointmentBool)
                @Html.CheckBoxFor(m => m.ColumnsNeeded.AppointmentBool)
            </td>
        </tr>
        <tr>
            <td class="PersonalDataInset">
                @Html.LabelFor(m => m.ColumnsNeeded.DivisionBool)
                @Html.CheckBoxFor(m => m.ColumnsNeeded.DivisionBool)
            </td>
        </tr>
    </table>
</div>

I have tried solutions from:

jQuery if checkbox is checked

jQuery, checkboxes and .is(":checked")

How to check whether a checkbox is checked in jQuery?

But unfortunately they don't work for me, i don't know why.

Community
  • 1
  • 1
Vadim.K
  • 89
  • 12
  • 4
    `show` is a method, call it with parenthesis... => `jQuery.show()` – Rayon Sep 27 '16 at 07:52
  • 4
    It should be `$(this).is(":checked")` better use `this.checked`, You just need to use `$("#ShowPersonalDataList").change(function () { $(".PersonalDataInset").toogle(this.checked); });` – Satpal Sep 27 '16 at 07:52
  • To check the `checked` state, go with `this.checked`... – Rayon Sep 27 '16 at 07:53
  • 1
    @Satpal just want to clarify what is wrong with `$(this).is(":checked")` is not the same as `this.checked` just the other is jQuery and the other is plain? – guradio Sep 27 '16 at 07:54
  • 2
    @guradio, Its simple and When native object is providing you the required data, Why do you need to create a jQuery object using `$(this)` then invoking the method – Satpal Sep 27 '16 at 07:56
  • @Rayon Now i feel like i am retarded, ty) – Vadim.K Sep 27 '16 at 08:03

1 Answers1

1

You have a wrong selector to identify state of element as checked/unchecked. you can use this.checked to get checked state and use it as argument to .toggle():

$("#ShowPersonalDataList").change(function () {
   $(".PersonalDataInset").toggle(this.checked);
});
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125