Solved

javascript check for radio buttons

Posted on 2012-03-18
2
213 Views
Last Modified: 2012-03-21
we have a javascript being passed dynamically into a webform, we insert data into the webform, but we need to check and select a radio button, how can we do this ?

 var formContent = {
"0":"905",
"9":"",
"10":"test123"
}

here is the code inserting the form values


var form = document.getElementById('Form1'); 

	for (var o in formContent) 
	{
		form.elements[o].value=formContent[o];
	}
                

Open in new window

0
Comment
Question by:goodluck11
2 Comments
 
LVL 7

Expert Comment

by:micropc1
ID: 37736292
Here are a couple of custom functions I've written to do this...

setRadioValue = function (radioName, newValue) {
	var radioObj = document.getElementsByName(radioName);
	if(radioObj != null) {
		var radioLength = radioObj.length;

		if(radioLength == undefined) {
			radioObj.checked = (radioObj.value == newValue.toString());
		} else {
			for(var i = 0; i < radioLength; i++) {
				radioObj[i].checked = false;
				if(radioObj[i].value == newValue.toString()) {
					radioObj[i].checked = true;
				}
			}
		}
	}
}


getRadioValue = function (radioName) {
	var myRadios = document.getElementsByName(radioName);
	var retVal = '';
	
	for (var i = 0; i < myRadios.length; i++) {
		if (myRadios[i].checked == true) {
			retVal = myRadios[i].value;
		}
	} 
	
	return retVal;
}

Open in new window


You would use them like this....

Test1 <input type="radio" name="t" id="t" value="test1">
Test1 <input type="radio" name="t" id="t" value="test2">
Test1 <input type="radio" name="t" id="t" value="test3">

<input type="button" onclick="alert (getRadioValue('t'));" value="a">
<input type="button" onclick="setRadioValue('t', 'test2');" value="b">

Open in new window

0
 
LVL 11

Accepted Solution

by:
Amar Bardoliwala earned 500 total points
ID: 37737353
Hello goodluck1,

you can try following

var type = form.elements[o].type

if(type=="checkbox")
{
   form.elements[o].checked = formContent[o];
}

Open in new window


Hope this will help you.

Thank You.

Amar Bardoliwala
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

823 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