Solved

uncheck, check RadioButton little javascript function

Posted on 2011-03-10
4
536 Views
Last Modified: 2013-11-19
hi there,
I need a little function to work the following way:
I have two RadioButtons
<asp:RadioButton ID="rdYes" onClick="uncheckRadio()" runat="server" Text="Yes" Font-Names="Verdana, Arial, Helvetica" Font-Size="10pt" />
            <asp:RadioButton ID="rdNo" onClick="uncheckRadio()" runat="server" Text="No" Font-Names="Verdana, Arial, Helvetica" Font-Size="10pt" />

and what i need the function to do is when Yes is Checked then NO should be unchecked and vice versa. One RadioButton should be checked at the time.
I know a RadioButtonList will take care of this for me but i have some other events and i need them individually.
Thanks,
COHFL
           
0
Comment
Question by:COHFL
  • 2
4 Comments
 
LVL 6

Expert Comment

by:Webmonkey
ID: 35095853
Try adding this:

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

Open in new window

0
 

Author Comment

by:COHFL
ID: 35095909
How do i call it?

OnClick="setCheckedValue(??,??)
0
 
LVL 6

Accepted Solution

by:
Webmonkey earned 250 total points
ID: 35095974
Nevermind... this is better

<input id="rdYes" name="yes" type="radio" value="yes" onclick="document.getElementById('rdNo').checked = false;" />
<input id="rdNo" name="no" type="radio" value="no" onclick="document.getElementById('rdYes').checked = false;" />

Open in new window


0
 
LVL 82

Expert Comment

by:leakim971
ID: 35096389
You don't want to use RadioButtonList : http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.radiobuttonlist(v=vs.80).aspx

Else why not use :
<input type="radio" name="yesorno" ID="rdYes" value="Yes" />Yes<br>
<input type="radio" name="yesorno" ID="rdNo" value="No" />No<br>

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now