Link to home
Start Free TrialLog in
Avatar of Guru Ji
Guru JiFlag for Canada

asked on

uncheck radiobutton onclick event using javascript

I have following radiobuttons
<asp:RadioButton ID="radV" runat="server"  Text="V"  GroupName="VS"  />
<asp:RadioButton ID="radS" runat="server"  Text="S"  GroupName="VS"   />

They work fine when I select on, other gets deselected.

What I want is that if someone clicks again on the selected one then it should deselect that one (nothing should be selected)

If I can do this via Javascript that would be great and any help is appreciated.

Thanks
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

<asp:RadioButton ID="radV" runat="server"  Text="V"  GroupName="VS" OnClientClick="javascript:checkuncheck(this);" />

// In Javascript
function checkuncheck(obj)
{
            obj.checked = !obj.checked;
}
Avatar of Guru Ji

ASKER

using your code, I can't even select any radiobutton.
The problem is that when you click on a radio button, by the time it gets to the javascript, it's state is 'checked'.  also, since the default behavior of a radio button is to stay checked, that is always what it's state will be when clicked.  So you can get around this by using a double click event.

function checkuncheck(obj)
{
	if(obj.checked){
		obj.checked=false;
	}
} 

Open in new window


<asp:RadioButton ID="radV" runat="server"  Text="V"  GroupName="VS" ondblclick="checkuncheck(this)"  />
<asp:RadioButton ID="radS" runat="server"  Text="S"  GroupName="VS"   ondblclick="checkuncheck(this)" />

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Guru Ji

ASKER

Sorry Barry62 your solution doesn't work
Yes, it does!  Did you copy and paste it and run it yourself?  Did you DOUBLE-CLICK on the radio buttons to unselect them?!  

My answer works.  I tested it myself.
Avatar of Guru Ji

ASKER

Yes I did double click it and also tested it. I tested yours first before testing the last one.

Honestly it didn't work on my side and I am using IE.
I just tested it with IE.  It works.  Clearly you aren't double-clicking correctly.  Or do you have javascript turned off?
What if informaniac tested my solution?  I wonder if it would work for him?
Avatar of Guru Ji

ASKER

No I tested informaniac solution and it worked and its same browser and same page.
That's not what I asked.  Could infomaniac test MY solution?  If it works for him, then it works period.  Fair enough?
@Barry62: Please don't get me wrong but, It doesn't work.

However it does work when instead of

<asp:RadioButton

I put a

<input type="radio"
OK.  Fair enough.  I suppose .Net doesn't handle javascript the same way as plain html.  I wouldn't know that.