change a select based on another select - easy points

I have two select boxes... something like this:

            <select name="client" id="input_client" onchange="javascript:sndReqClient('update_client_defaults')" \>
            <option value="American Steel">American Steel</option>
            <option value="BMC">BMC</option>
            <option value="Brian Weiss">Brian Weiss</option>
            <option value="BSF">BSF</option>
            </select>

            <select name="rate" id="input_rate">
            <option value="4">BSF Special</option>
            <option value="5">Home User</option>
            <option value="3">Web</option>
            <option value="2">Block Hours</option>
            <option value="1">Regular</option>
            </select>

When you select a client, the script sends a request to a seperate program that looks up the default rate for that client, and returns the id for the corresponding rate.
Say it returns a 4 - then "BSF Special" should be selected, if it returns a 2 "Block Hours" should become selected, etc.

Right now I'm using this, but I know its not right... it changes the value of the selec but not to the correct value...

######## Javascript ############

        r=document.getElementById('input_rate');
        r.selectedIndex = response;

###########################

response if the number that should match up to the rate id (that part is working). The problem with this code is that, say you were to select 'BSF' from the client list, the program would run and return the number 4. The this javascript would make the rate called "Regular" selected, because it is option number 4 (counting from 0). How can I get it to be based on the value of the option, not the order that the option appears?
LVL 5
blacksixAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nabsolCommented:
Hi
Try running this in your browser. What i have done assumed is that response is "client" dropdowns selected index. Hope code helps.

<html>
<head>
<script language="JavaScript">
function sndReqClient(update_client_defaults)
{
  response = update_client_defaults.selectedIndex;
  r=document.getElementById('input_rate');
  for (i=0; i <r.options.length;i++)
  {
     if (r.options[i].value == response)
     {
         r.options[i].selected = true;
         break;
     }
  }

}
</script>
</head>

<body>
<select name="client" id="input_client" onchange="javascript:sndReqClient(this)" \>
  <option value="-1">Select Client</option>
  <option value="American Steel">American Steel</option>
   <option value="BMC">BMC</option>
   <option value="Brian Weiss">Brian Weiss</option>
   <option value="BSF">BSF</option>
</select>

<select name="rate" id="input_rate">
  <option value="-1">Select</option>
  <option value="4">BSF Special</option>
  <option value="5">Home User</option>
  <option value="3">Web</option>
  <option value="2">Block Hours</option>
  <option value="1">Regular</option>
</select>
</body>
</html>

By Nab
nabsolCommented:
Hi
Another example. If response is hardcoded to 5 always home user will be selected if any option will be selected from client drop down.

<html>
<head>
<script language="JavaScript">
function sndReqClient(update_client_defaults)
{
  response = 5;
  r=document.getElementById('input_rate');
  for (i=0; i <r.options.length;i++)
  {
     if (r.options[i].value == response)
     {
         r.options[i].selected = true;
         break;
     }
  }

}
</script>
</head>

<body>
<select name="client" id="input_client" onchange="javascript:sndReqClient(this)" \>
  <option value="-1">Select Client</option>
  <option value="American Steel">American Steel</option>
   <option value="BMC">BMC</option>
   <option value="Brian Weiss">Brian Weiss</option>
   <option value="BSF">BSF</option>
</select>

<select name="rate" id="input_rate">
  <option value="-1">Select</option>
  <option value="4">BSF Special</option>
  <option value="5">Home User</option>
  <option value="3">Web</option>
  <option value="2">Block Hours</option>
  <option value="1">Regular</option>
</select>
</body>
</html>

By Nab

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pravin AsarPrincipal Systems EngineerCommented:
Here is an example, how you can set the auto select (set value)  rate based on the selection done for for client select box.
Look at the use of switch statement. This you need to modify to suit your requirements.. code all the cases.

<body>
<script language="javascript">
function sndReqClient (srcFld) {
      if (!srcFld) { return (-1); }
      switch (srcFld.value) {
            case 'American Steel':
                  return (1);
            case 'BMC':
                  return (2);
            case 'BSF':
                  return (3);
            default:
                  return (-1);
      }
}
</script>
<form>

<select name="client" id="input_client"
onchange="this.form.rate.value=sndReqClient(this);">
          <option value="American Steel">American Steel</option>
          <option value="BMC">BMC</option>
          <option value="Brian Weiss">Brian Weiss</option>
          <option value="BSF">BSF</option>
          </select>

          <select name="rate" id="input_rate">
      <option value="-1">Not Selected</option>
          <option value="4">BSF Special</option>
          <option value="5">Home User</option>
          <option value="3">Web</option>
          <option value="2">Block Hours</option>
          <option value="1">Regular</option>
          </select>
</form>  
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.