Solved

javascript check for radio buttons

Posted on 2012-03-18
2
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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

634 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