Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I have a asp.net website in c#.

In one page i have few check boxes only a table but not in a repeater or any grid.

I have added one more checkbox to 'Select All' or to 'Un Select All'

For that i wrote jquery.

<script type="text/javascript">
        $(document).ready(function () {
            $('#selecctall').click(function (event) {  //on click 
                if (this.checked) { // check select status
                    $("CheckBox.checkbox1").each(function () { //loop through each checkbox
                        this.Checked = true;  //select all checkboxes with class "checkbox1"               
                    });
                } else {
                    $('.checkbox1').each(function () { //loop through each checkbox
                        this.Checked = false; //deselect all checkboxes with class "checkbox1"                       
                    });
                }
            });

        });
    </script>

Table
-----
<tr>
                    <td style="background-color: Silver;">
                        <asp:Label ID="lblmsrig" runat="server" Text="Rig" />
                    </td>
                    <td style="font-weight: bold;">
                        <asp:CheckBox CssClass="checkbox1" ID="chkmsrigins" runat="server" Text="Insert" />
                    </td>
                    <td style="font-weight: bold;">
                        <asp:CheckBox CssClass="checkbox1" ID="chkmsrigview" runat="server" Text="View" />
                    </td>
                    <td style="font-weight: bold;">
                        <asp:CheckBox CssClass="checkbox1" ID="chkmsrigupdate" runat="server" Text="Update" />
                    </td>
                    <td style="font-weight: bold;">
                        <asp:CheckBox CssClass="checkbox1" ID="chkmsrigdel" runat="server" Text="Delete" />
                    </td>
                </tr>


but i'm not getting elements by its CssClass name.

Can any one plz help me.

Thanks
Posted

I would like to suggest two changes
1. Change
JavaScript
$("CheckBox.checkbox1")

to
JavaScript
$(".checkbox1")

2. Change
JavaScript
this.Checked = true;

to
JavaScript
this.checked = true;


Hope, it helps :)
 
Share this answer
 
Comments
abdul subhan mohammed 14-Jul-15 7:06am    
I already tried this..
But it's not working
$('[class$="checkbox"]').val();
 
Share this answer
 
You are mixing server side markup with client side HTML...
There is no such tag 'CheckBox' in HTML - the ASP.NET control called CheckBox will render to an input tag in the final HTML code, so the proper selector for your checkboxes should be based on 'input[type="checkbox"'...
Fortunately jQuery has a shortcut to select all input elements of type checkbox...
JavaScript
$(':checkbox')

https://api.jquery.com/checkbox-selector/[^]
 
Share this answer
 
C#
$("#selecctall").click(function () { //on click
          if ($(this).prop('checked') == true) {// check select status
              $(".checkbox1").each(function () {//loop through each checkbox
                  this.checked = true; //select all checkboxes with class "checkbox1"
              });
          }
          else {
              $(".checkbox1").each(function () {//loop through each checkbox
                  this.checked = false;//deselect all checkboxes with class "checkbox1"
              });
          }
      });

Hope this code will help you :)
 
Share this answer
 
Thanks everyone for helping me...

I found this and its working awesome

XML
<p>
<b>It is simple, (asp:CheckBox) is rendered as a (span) with label and input field with type="check". So when you are trying to say $(".chkboxClass").removeAttr('checked'); you are actually calling the (span) tag and that is the reason it is not working. To make it works just add:
</b></p>


<script type="text/javascript">
        $(document).ready(function () {
            $('#selecctall').click(function (event) {  //on click 
                if (this.checked) { // check select status                    
                    $(".checkbox1 :input").attr("checked", "checked");
                } else {
                    $(".checkbox1 :input").removeAttr('checked');
                }
            });

        });
    </script>
 
Share this answer
 

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