Thomas
Thomas

Reputation: 34188

check uncheck all checkbox of checkboxlist using jquery ASP.Net

i was trying to check uncheck all option of checkboxlist programmatically but my code is not working. here is snippet

 $(document).ready(function () {
    $('#myLink').click(function () {
        $("INPUT[id^='chkFileds']").attr('checked', $('#chkFileds').is(':checked'));
    });
});

what is wrong in my code. i want that when user click on check hyperlink then all option of checkbox list will be checked and when click again on the same link then all option of checkbox list will be uncheck. please guide me how to do it. thanks

UPDATE:

thanks for ur answer but i am not talking about check rather checkbox list what has many checkbox inside.it looks like....he i am giving the html of checkboxlist

<table style="font-weight:bold;" id="chkCountry">
    <tbody>
        <tr>
            <td>
                <input type="checkbox" value="GB" name="chkCountry$0" id="chkCountry_0">
                <label for="chkCountry_0">UK</label>
            </td>
            <td>
                <input type="checkbox" value="US" name="chkCountry$1" id="chkCountry_1">
                <label for="chkCountry_1">USA</label>
            </td>
        </tr>
    </tbody>
</table>

Upvotes: 1

Views: 13115

Answers (2)

James Hill
James Hill

Reputation: 61802

You markup is still missing some of the relevant code needed to answer the question properly, but, assuming that myLink is a checkbox, the code below will work.

$(document).ready(function () {
   $('#myLink').click(function () {
      $("INPUT[id^='chkCountry_']").attr('checked', $('#myLink').is(':checked'));
   });
});

Here's a working fiddle.

Note: You're code is not working for several reasons. Mainly, your selector, chkFileds, doesn't relate to the markup you posted in any way.

Additional Info:

Here's a quick rundown of the attribute selector operators:

=  is exactly equal
!= is not equal
^= starts with
$= ends with
*= contains

Upvotes: 6

adeneo
adeneo

Reputation: 318182

If your selectors are correct, and they do seem a bit strange, maybe something like this:

$(document).ready(function () {
    $('#myLink').on('click', function () {
        var check = $('#chkFileds').is(':checked') ? false:true;
        $("INPUT[id^='chkFileds']").prop('checked', check);
    });
});

FIDDLE

What exactly do you mean by "all" options, you are targeting an ID, and it's unique?

Upvotes: 0

Related Questions