Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a html table in my view. I apply a checkbox to select all check boxes of my table. here is my code :
 <table id="dtsResult" class="table table-striped table-bordered" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>SelectAll<input type="checkbox" id="CheckAllRes" /></th>
                <th>Unit</th>
             </tr>
        </thead>
        <tbody>
            @foreach (var item in ViewBag.Result)
            {
                <tr>
                    <td><input type="checkbox" id=@item.RecordID name="res" class="chkres" /></td>                   
                    <td> @item.UnitNo</td>
                </tr>
            }
        </tbody>
    </table>
Here is my Jquery Function
 $(document).ready(function () { 
          $('#CheckAllRes').on("click", function (){          
            var chk = $(this).is(':checked');            
            $(".chkres", "#dtsResult").each(function () {
                if (chk) {                   
                    $(this).attr("checked", true);
                }
                else {
                    $(this).attr("checked", false);
                }
            });
        });
  });

Now my problem is when i click first time then it checks all records and same for remove. But when i click again to select all . then no checkbox is selected.
i am new to jquery. Please help.
Thanks
Posted
Updated 9-Nov-15 20:06pm
v2
Comments
jaket-cp 10-Nov-15 4:38am    
The HTML and jQuery appear to be okay - have created a fiddle and it is checking and unchecking all as expected
http://jsfiddle.net/eyLey1v7/ - let me know if I am missing something...

Just a small point on the jQuery, rather than looping through #dtsResult, loop #dtsResultBody instead. Reason being #CheckAllRes will not get checked or unchecked again.

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900