0

This is the script I am working with.

$(document).ready(function () {
    $('#selectall').click(function () {
        $('.selectedId').prop('checked', isChecked('selectall'));
    });
});

function isChecked(checkboxId) {
    var id = '#' + checkboxId;
    return $(id).is(":checked");
}

function resetSelectAll() {
    // if all checkbox are selected, check the selectall checkbox
    // and viceversa
    if ($(".selectedId").length === $(".selectedId:checked").length) {
        $("#selectall").attr("checked", "checked");
    } else {
        $("#selectall").removeAttr("checked");
    }

    if ($(".selectedId:checked").length > 0) {
        $('#edit').attr("disabled", false);
    } else {
        $('#edit').attr("disabled", true);
    }
}

And this is the cshtml view.

<table id="notificationsTableId">
    <thead>
    <tr role="row">
        <th rowspan="1" colspan="1">
            <input type="checkbox" id="selectall" />
        </th>
    </tr>
    </thead>
    <tbody>
    <tr class="results-table-row odd">
        <td>
            <input type="checkbox" class="selectedId" name="selectedId" onclick="resetSelectAll();"/>
        </td>
    </tr>
    <tr class="results-table-row even">
        <td>
            <input type="checkbox" class="selectedId" name="selectedId" onclick="resetSelectAll();"/>
        </td>
    </tr>
    <tr class="results-table-row even odd">
        <td>
                <input type="checkbox" class="selectedId" name="selectedId" onclick="resetSelectAll();" />
        </td>
    </tr>
    </tbody>
</table>

Right now the select button will work like it should, if i check it it will check all the dates inside that table. But what I want to do is I want that button to select all the Check-boxes on the page. But if I give a checkbox the same ID inside my foreach loop like this:

@foreach (var date in group)
{
    var isoDate = date.ToString("yyMMdd");
    var day = date.ToString("ddd", new CultureInfo("sv-SE")).Substring(0, 2);
    <label style="padding-left: 10px">
        <input type="checkbox" id="selectedId" name="isoDate" value="@isoDate"/>@day-@isoDate
    </label>
}

It doesn't work, it will still only select those 3 check-boxes on the top of the page. I would assume it would check all the check-boxes correct? It ends up like this: enter image description here

1
  • 1
    ID should be unique, your HTML markup is wrong Commented Aug 31, 2015 at 8:59

1 Answer 1

2

You are using selectedId as id instead of class like other checkboxes. Use it as class attribute as shown below

@foreach (var date in group)
{
    var isoDate = date.ToString("yyMMdd");
    var day = date.ToString("ddd", new CultureInfo("sv-SE")).Substring(0, 2);
    <label style="padding-left: 10px">
        <input type="checkbox" class="selectedId" name="isoDate" value="@isoDate"/>@day-@isoDate
    </label>
}
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.