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
LVL 11
Guru JiAsked:
Who is Participating?
 
Obadiah ChristopherConnect With a Mentor Commented:
<asp:RadioButton ID="radV" runat="server"  Text="V"  GroupName="VS" onclick="javascript:return checkuncheck(this,this.checked);"  />
        <asp:RadioButton ID="radS" runat="server"  Text="S"  GroupName="VS" onclick="javascript:return checkuncheck(this,this.checked);"  />

Open in new window

<script type="text/javascript" language="javascript">
        var isChecked = false;
        var currCheckBox = null;
        function checkuncheck(obj, checked) {
            if (obj != currCheckBox) {
                isChecked = true;
                currCheckBox = obj;
                return true;
            } if (!isChecked && checked) {
                isChecked = true;
            }
            else {
                obj.checked = false;
                isChecked = false;
            }
            return true;
        }
    </script>

Open in new window

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

// In Javascript
function checkuncheck(obj)
{
            obj.checked = !obj.checked;
}
0
 
Guru JiAuthor Commented:
using your code, I can't even select any radiobutton.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Barry62Commented:
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

0
 
Guru JiAuthor Commented:
Sorry Barry62 your solution doesn't work
0
 
Barry62Commented:
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.
0
 
Guru JiAuthor Commented:
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.
0
 
Barry62Commented:
I just tested it with IE.  It works.  Clearly you aren't double-clicking correctly.  Or do you have javascript turned off?
0
 
Barry62Commented:
What if informaniac tested my solution?  I wonder if it would work for him?
0
 
Guru JiAuthor Commented:
No I tested informaniac solution and it worked and its same browser and same page.
0
 
Barry62Commented:
That's not what I asked.  Could infomaniac test MY solution?  If it works for him, then it works period.  Fair enough?
0
 
Obadiah ChristopherCommented:
@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"
0
 
Barry62Commented:
OK.  Fair enough.  I suppose .Net doesn't handle javascript the same way as plain html.  I wouldn't know that.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.