JavaScript Basics
- Getting Id of the Checkbox in JavaScript
The below code snippet demonstrates toggling : On selecting one checkbox the other checkbox if selected gets unselected and vice-versa.
1: <script type="text/javascript" language=javascript>
2:
3: function fn() {
4: var checkbox = document.getElementById('<%= cbx1.ClientID %>');
5: if (checkbox != null && checkbox.checked == true) {
6: var checkbox2 = document.getElementById('<%= cbx2.ClientID %>');
7: if (checkbox2 != null && checkbox.checked == true) {
8: checkbox2.checked = false;
9: }
10: }
11: }
12:
13: function fn2() {
14: var checkbox = document.getElementById('<%= cbx2.ClientID %>');
15: if (checkbox != null && checkbox.checked == true) {
16: var checkbox2 = document.getElementById('<%= cbx1.ClientID %>');
17: if (checkbox2 != null && checkbox.checked == true) {
18: checkbox2.checked = false;
19: }
20: }
21: }
22:
23: </script>
24:
25: <asp:CheckBox ID="cbx1" onclick="fn()" runat="server" />
26: <asp:CheckBox ID="cbx2" onclick="fn2()" runat="server" />