Solved

javascript check for radio buttons

Posted on 2012-03-18
2
218 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

749 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