Vadim.K
Vadim.K

Reputation: 89

JQuery hide\show if checkbox is checked

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.

Upvotes: 0

Views: 1180

Answers (1)

Milind Anantwar
Milind Anantwar

Reputation: 82231

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);
});

Upvotes: 1

Related Questions