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!

Link to comment
Share on other sites

// 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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.