[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

uncheck radiobutton onclick event using javascript

Posted on 2012-08-24
13
Medium Priority
?
2,436 Views
Last Modified: 2012-08-26
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
0
Comment
Question by:Guru Ji
  • 6
  • 4
  • 3
13 Comments
 
LVL 20

Expert Comment

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

// In Javascript
function checkuncheck(obj)
{
            obj.checked = !obj.checked;
}
0
 
LVL 11

Author Comment

by:Guru Ji
ID: 38330180
using your code, I can't even select any radiobutton.
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38331387
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 20

Accepted Solution

by:
informaniac earned 2000 total points
ID: 38331954
<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
 
LVL 11

Author Comment

by:Guru Ji
ID: 38332584
Sorry Barry62 your solution doesn't work
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38332799
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
 
LVL 11

Author Comment

by:Guru Ji
ID: 38332835
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
 
LVL 8

Expert Comment

by:Barry62
ID: 38332858
I just tested it with IE.  It works.  Clearly you aren't double-clicking correctly.  Or do you have javascript turned off?
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38332861
What if informaniac tested my solution?  I wonder if it would work for him?
0
 
LVL 11

Author Comment

by:Guru Ji
ID: 38332959
No I tested informaniac solution and it worked and its same browser and same page.
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38333684
That's not what I asked.  Could infomaniac test MY solution?  If it works for him, then it works period.  Fair enough?
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38334282
@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
 
LVL 8

Expert Comment

by:Barry62
ID: 38334460
OK.  Fair enough.  I suppose .Net doesn't handle javascript the same way as plain html.  I wouldn't know that.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month20 days, 12 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question