• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

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?
0
blacksix
Asked:
blacksix
  • 2
1 Solution
 
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
0
 
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
0
 
Pravin AsarCommented:
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>  
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now