By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,657 Members | 774 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,657 IT Pros & Developers. It's quick & easy.

Accessing the selected value in a combo box

P: 68
I have the task of adding a value to a textbox based on the selected value in a combo box. The trouble is, I cant seem to access the selected content in the combo box so as to store it in a variable, then compare it in a switch statement, in order to write the required output to the text box.


Here is the code i am using
Expand|Select|Wrap|Line Numbers
  1. function EnterUserID()
  2. {
  3.   var Name=document.MyForm.txtPerformedBy.value;
  4.   switch (Name)
  5.   {
  6.      case (Name="Keniemoka"):
  7.          document.MyForm.txtUserID.value="19";
  8.      break
  9.  
  10.      case (Name="Caludech"):
  11.          document.MyForm.txtUserID.value="24";
  12.      break
  13.  
  14.      case (Name="Harrichandrahp"):
  15.          document.MyForm.txtUserID.value="25";
  16.      break
  17.  
  18.      case (Name="Adelah"):
  19.          document.MyForm.txtUserID.value="26";
  20.      break
  21.  
  22.      case (Name="Francisff"):
  23.          document.MyForm.txtUserID.value="2";
  24.      break
  25.  
  26.      case (Name="Christophercr"):
  27.          document.MyForm.txtUserID.value="11";
  28.      break
  29.  
  30.      case (Name="msm"):
  31.          document.MyForm.txtUserID.value="12";
  32.      break
  33.  
  34.      default:
  35.          document.MyForm.txtUserID.value=Name;
  36.  
  37.      break
  38.   }
  39. }
when execuiting this code i am being posted to default which prints the last name in the combo list.
May 29 '08 #1
Share this Question
Share on Google+
3 Replies


gits
Expert Mod 5K+
P: 5,223
fix the wrong usage of the switch statement ... have a look at the following example:

Expand|Select|Wrap|Line Numbers
  1. switch (Name) {
  2. case "Keniemoka":
  3.     //your code
  4.     break;
  5. }
kind regards
May 29 '08 #2

100+
P: 428
If the options have values, just pass the value.

(most browsers will return the selected value without fishing for it-
document.MyForm.txtUserID.value= document.MyForm.txtPerformedBy.value

Or for older browsers:

Expand|Select|Wrap|Line Numbers
  1. function EnterUserID(){    
  2. var who=document.MyForm.txtPerformedBy;
  3. document.MyForm.txtUserID.value= who.options[who.selectedIndex].value;
  4. }
Your html option values are what get submitted, not the text

<select name="txtPerformedBy" size="1" onchange="EnterUserID">
<option value="19">Caludech</option>
<option value="24">Harrichandrahp</option>
</select>
May 29 '08 #3

P: 68
thanks guys I got through.
with the information that you gave me i realized that i was doing things the hard way and change my function to the following.

Expand|Select|Wrap|Line Numbers
  1. function EnterUserID(SourceFieldID,y)
  2. {
  3.   var Name=document.getElementById(SourceFieldID).value;
  4.  
  5.   if (Name=="")
  6.   {
  7.       alert ("Please select a valid entry");
  8.   }
  9.   document.getElementById(y).value=Name;
  10. }
you can tell by this that i am a novis but thanks to you i feel like a geek

thanks also for the tip on the syntax errors in the switch i cant say that i didn't learn something and it just might come in handy some day soon.
May 29 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.