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.

6
  • 4
    show is a method, call it with parenthesis... => jQuery.show() Commented Sep 27, 2016 at 7:52
  • 4
    It should be $(this).is(":checked") better use this.checked, You just need to use $("#ShowPersonalDataList").change(function () { $(".PersonalDataInset").toogle(this.checked); }); Commented Sep 27, 2016 at 7:52
  • To check the checked state, go with this.checked... Commented Sep 27, 2016 at 7: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? Commented Sep 27, 2016 at 7: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 Commented Sep 27, 2016 at 7:56

1 Answer 1

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);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.