Accessing the selected value in a combo box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KingKen
    New Member
    • Feb 2008
    • 68

    #1

    Accessing the selected value in a combo box

    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
    Code:
    function EnterUserID()
    {
      var Name=document.MyForm.txtPerformedBy.value;
      switch (Name)
      {
         case (Name="Keniemoka"):
             document.MyForm.txtUserID.value="19";
         break
         
         case (Name="Caludech"):
             document.MyForm.txtUserID.value="24";
         break
         
         case (Name="Harrichandrahp"):
             document.MyForm.txtUserID.value="25";
         break
         
         case (Name="Adelah"):
             document.MyForm.txtUserID.value="26";
         break
         
         case (Name="Francisff"):
             document.MyForm.txtUserID.value="2";
         break
         
         case (Name="Christophercr"):
             document.MyForm.txtUserID.value="11";
         break
         
         case (Name="msm"):
             document.MyForm.txtUserID.value="12";
         break
    
         default:
             document.MyForm.txtUserID.value=Name;
    
         break
      }
    }
    when execuiting this code i am being posted to default which prints the last name in the combo list.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    fix the wrong usage of the switch statement ... have a look at the following example:

    [CODE=javascript]switch (Name) {
    case "Keniemoka" :
    //your code
    break;
    }[/CODE]
    kind regards

    Comment

    • mrhoo
      Contributor
      • Jun 2006
      • 428

      #3
      If the options have values, just pass the value.

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

      Or for older browsers:

      Code:
      function EnterUserID(){	
      var who=document.MyForm.txtPerformedBy;
      document.MyForm.txtUserID.value= who.options[who.selectedIndex].value;
      }
      Your html option values are what get submitted, not the text

      <select name="txtPerfor medBy" size="1" onchange="Enter UserID">
      <option value="19">Calu dech</option>
      <option value="24">Harr ichandrahp</option>
      </select>

      Comment

      • KingKen
        New Member
        • Feb 2008
        • 68

        #4
        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.

        Code:
        function EnterUserID(SourceFieldID,y)
        {
          var Name=document.getElementById(SourceFieldID).value;
          
          if (Name=="")
          {
              alert ("Please select a valid entry");
          }
          document.getElementById(y).value=Name;
        }
        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.

        Comment

        Working...