Solved

javascript check for radio buttons

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

743 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

14 Experts available now in Live!

Get 1:1 Help Now