p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   Javascript How-To (http://p2p.wrox.com/javascript-how-87/)
-   -   display value of selected radio in the textbox (http://p2p.wrox.com/javascript-how/8835-display-value-selected-radio-textbox.html)

hosefo81 February 1st, 2004 02:21 AM

display value of selected radio in the textbox
 
i want the value of the selected radio button to be displayed on the textboxt.
below are my code and there is an error.
i believe it is in the function1().Please correct my code.

<html>
<head>
    <title>Search Employee Data</title>
<script language="javascript">
function function1()
{
    document.searchempform.text1.value =document.searchempform.search_emptype[document.searchempform.search_emptype.selectedInde x].value

}
</script>
</head onload="document.searchempform.reset()">

<body>
<form name="searchempform">

<b>How do you want to search? </b><br>
<input type="radio" name="search_emptype" value="searchempid" onclick="function1()">Search by Employee ID<br>
<input type="radio" name="search_emptype" value="searchempfname" onclick="function1()">Search by Employee Name<br>
<input type="radio" name="search_emptype" value="searchemplname" onclick="function1()">Search by Employee Name<br>

<input type="text" name="text1" size="10">
<input id="empsearchbut" type="button" name="empsearch" size="10" value="Search" onclick="function1()" >

</form>
</body>
</html>


joefawcett February 1st, 2004 07:57 AM

Your code is correct for list boxes. For radio button group you need to loop through them and pick the checked one.
Code:

function getRadioFromGroup(GroupName)

  var colButtons = document.getElementsByName(GroupName)
  for (var i = 0; i < colButtons.length; i++)
  {
    if (colButtons[i].checked) return colButtons[i];
  }
  return null;
}

//Function to call from empsearchbutton
onclick="showValue(getRadioFromGroup('search_emptype'));

To show value immediately as you want then change your onclick to:
Code:

onclick="showValue(this);"
in your buttons and have showValue as:
Code:

function showValue(Button)
{
  if (Button)
  {
    document.searchempform.text1.value = Button.value;
  } 
}

--

Joe


All times are GMT -4. The time now is 01:04 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.