Jump to content

How to put returned radio value to an input text field?


ivytony

Recommended Posts

I found the source code for javascript to return a radio button value at here http://www.somacon.com/p143.php

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
if(!radioObj)
	return "";
var radioLength = radioObj.length;
if(radioLength == undefined)
	if(radioObj.checked)
		return radioObj.value;
	else
		return "";
for(var i = 0; i < radioLength; i++) {
	if(radioObj[i].checked) {
		return radioObj[i].value;
	}
}
return "";
}

 

But I want to have the returned value in an input text field named field1. How to do this?

 

thanks!

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
if(!radioObj)
	return false;
var radioLength = radioObj.length;
if(radioLength == undefined)
	if(radioObj.checked)
		return radioObj.value;
	else
		return false;
for(var i = 0; i < radioLength; i++) {
	if(radioObj[i].checked) {
		var target = document.getElementById("field1")
		if(target.setAttribute)
		{
			target.setAttribute("value", radioObj[i].value)
		}
		else
		{
			target.value = radioObj[i].value
		}
	}
}
return false;
}

<input type="text" name="field1" id="field1" />

 

I think this should work in firefox, and probably in IE, although I may be wrong. Let me know if it doesn't work.

 

Also, the code wasn't that great (lots of return "" which isn't great code), so I would suggest maybe avoiding that site in the future.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.