Click here to Skip to main content
15,905,914 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hi all, I am new to asp.net and trying to learn it,

I have created a simple page including a button and a checkboxes.

how to check checkbox is checked or not in javascript in asp.net
Posted

This is the simple javascript code to get the status of checkbox.

C#
var chkStatus1 = document.getElementById("chkbutton1");
var chkStatus2 = document.getElementById("chkbutton2");

if (chkStatus1.checked) {
    // do your implementation for if part
}
else if(chkStatus2.checked) 
{
   // do your implementation for else if part for checkbox2
}
else if(chkStatus2.checked && chkStatus2.checked)
{
   // do your implementation when two chechbox status is checked.
}
else
{
   // do your implementation for else part.
}
 
Share this answer
 
v3
Hi,
You can do it like this,
JavaScript
<script>
function checkcheckbox()
{
      //say for example you have checkbox with id="chk"
      var value = document.getElementById("<%= chk.ClientID %>").checked;
      // now value will have true or false. If true that means checkbox is checked and if value is false, that means checkbox is unchecked.
}
</script>

Hope it helps you.
 
Share this answer
 
v2
<script type="text/javascript">
        var hide1 = true;
        

        function hideColumn1(tableId, colIndex) {
            var table = document.getElementById(tableId);

            if (table != null) {
                for (i = 0; i < table.rows.length; i++) {
                    if (hide1)
                        table.rows[i].cells[colIndex - 1].style.display = 'none';
                    else
                        table.rows[i].cells[colIndex - 1].style.display = '';
                }
                hide1 = !hide1;
            }
    </script> 

<asp:repeater id="rpt" runat="server" >

       <headertemplate>
        <table  id="myTable" >
          <tr>
                <th >
                </th>
                  <th >
                </th>
                 .
                 .
                 .
        </tr>
           </table></headertemplate></asp:repeater>

<input id="Checkbox1" type="checkbox"  önclick="hideColumn1('myTable', 1)"  runat="server" checked="checked" />
<input id="Checkbox1" type="checkbox"  önclick="hideColumn1('myTable', 2)"  runat="server" checked="checked" />
<input id="Checkbox1" type="checkbox"  önclick="hideColumn1('myTable', 3)"  runat="server" checked="checked" />
.
.
.
 
Share this answer
 
v3
It's the easiest way you can go for

when-checkbox-is-checked-show-message-in-javascript[^]
 
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