Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

update textfield from radio group value 2

Status
Not open for further replies.

kiwieur

Technical User
Apr 25, 2006
200
GB
Hi people,
I am trying to populate a textfield value based on which of three radio buttons are checked. this is the code I have tried but nothing happens when I select the buttons

Code:
<script language="JavaScript">
function showValues(){
  if (document.frmReason.optAction.checked.value ==1){
    document.frmReason.txtAction.value ="C/Comp"
  }
  else{
  if (document.frmReason.optAction.checked.value ==2){
    document.frmReason.txtAction.value ="Today"
  }
  else{
  if (document.frmReason.optAction.checked.value ==3){
    document.frmReason.txtAction.value ="Tomorrow"
  }
  else{
  	document.frmReason.txtAction.value =""
  			}
  		}
	}
}
</script>

and this is what i have to update the values

Code:
<input name="optAction" type="radio" onClick="showValues()" value="1" />
            C/Comp</label></td>
          <td width="69"><input name="optAction" type="radio" onClick="showValues()" value="2"/>
Today</td>
          <td width="94"><input name="optAction" type="radio" onClick="showValues()" value="3"/>
Tomorrow</td>
          <td width="222"><input name="txtAction" type="text" id="txtAction" size="12" maxlength="12" />

I know I have something wrong but I am not sure what it is, any help would be appreciated

Regards

Paul

 
Code:
for (var i=0; i < document.frmReason.optAction.length; i++)
{
   if (document.frmReason.optAction[i].checked)
   {
      var selectedVal= document.siso.ampm[i].value;

      if (selectedVal == "1"){
        document.frmReason.txtAction.value ="C/Comp"
      }
      else if (selectedVal == "2"){
        document.frmReason.txtAction.value ="Today"
      }
      else if (selectedVal == "3"){
        document.frmReason.txtAction.value ="Tomorrow"
      }
      else{
         document.frmReason.txtAction.value =""
      }
   }      
}
 
Hi,

Firstly (you possibly just didn't include this), but your form needs to be wrapped in form tags with the name of "frmReason".

Personally I wouldn't get the value of the radio button within the function. I would pass the radio button value to the function. I also removed the nesting of if and else tags and made use of 'else if'...

Code:
<head>
<script type="text/javascript" language="javascript">
function showValues(val){
  if (val == 1){ document.frmReason.txtAction.value ="C/Comp" }
  else if (val == 2){ document.frmReason.txtAction.value ="Today" }
  else if (val == 3){ document.frmReason.txtAction.value ="Tomorrow" }
}
</script>
</head>

<body>
<form name="frmReason">
	<p><input name="optAction" id="optAction" type="radio" onClick="showValues(this.value)" value="1" /> C/Comp</p>
	<p><input name="optAction" id="optAction" type="radio" onClick="showValues(this.value)" value="2" /> Today</p>
	<p><input name="optAction" id="optAction" type="radio" onClick="showValues(this.value)" value="3"/> Tomorrow</p>
	<p><input name="txtAction" type="text" id="txtAction" size="12" maxlength="12" /></p>
</form>
</body>

Any reason why you don't just put what you want to print into the textbox as the value of each radio button? This way you could shorten your code into something similar to this (although you still may wish to put the javascript into a single function)...

Code:
<form name="frmReason">
	<p><input name="optAction" type="radio" onClick="document.frmReason.txtAction.value=this.value" value="C/Comp" /> C/Comp</p>
	<p><input name="optAction" type="radio" onClick="document.frmReason.txtAction.value=this.value" value="Today" /> Today</p>
	<p><input name="optAction" type="radio" onClick="document.frmReason.txtAction.value=this.value" value="Tomorrow"/> Tomorrow</p>
	<p><input name="txtAction" type="text" id="txtAction" size="12" maxlength="12" /></p>
</form>

Hope this helps,

Chris
 
Kendel/Chris,

firstly thank you both for the rapid responses.

Chris I did have the form but did not show the code

I have tried all the suggestions from both of you and they work perfectly.

I have opted for Chris's second solution, but stars for you both

Regards

Paul

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top