Friday, June 13, 2008

To check and uncheck a radio button

Code to check and uncheck a radio button,

Logic 1:
Add below code to the control
onclick='this.chk = this.chk ? this.checked = !this.chk : this.checked'
OR

Explanation:

A property by name "chk" is created to "this" object and is assigned the value of standard "checked". It stores the current state of the radio button. Based on this we check and uncheck the radio button. (You can use any name in place of "chk")

Logic 2:


Explanation:
Declare a global variable and set it to null initially. Call this function in the radio button control onclick event and pass "this" as parameter. When radio button is checked for first time, the if condition is true and hence it is checked. The global variable's value is now changed. Now when you check the radio button, if condition is false and hence else part is executed where radio button checked property is set to false. Here, global variable's value is again changed. This way, the radio button gets checked and unchecked alternatively.

No comments: