Javascript --- synchronizing two select lists

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Weston C

    Javascript --- synchronizing two select lists

    I'm trying to keep the selected / displayed option of two different
    select lists synchronized -- so when the user selects, say, the third
    option on one of them, the third option on the other is displayed:



    Here's the code I'm trying -- but that doesn't seem to work:

    function synchronizeSele cts(thisSelect, otherSelect)
    {
    // find the selected option in thisSelect
    i = 0;
    while(i < thisSelect.opti ons.length &&
    !(thisSelect.op tions[i].selected))
    i++;

    // if there is one, and it's not greater in # than
    // the number of options in the otherSelect
    if( (i != thisSelect.opti ons.length)
    && (i < otherSelect.opt ions.length) )
    {
    //then search the otherSelect for a selction
    //and turn it off
    for(j = 0; j < otherSelect.opt ions.length; j++)
    {
    otherSelect.opt ions[j].select = false;
    otherSelect.opt ions[i].defaultSelecte d = false;
    }
    //and then select i
    otherSelect.opt ions[i].select = true;
    otherSelect.opt ions[i].defaultSelecte d = true;
    }
    }

    It's called from an onchange even on both selects. For example:
    <select name="product" id="prod" class="smallsel ects"
    onchange="synch ronizeSelects(t his,document.ge tElementById('s upp'))">

    Any ideas what I may be doing wrong here?

    Thanks,

    Weston

    ~==~

    Taking Pictures During Dreams
    weston8[at]cann8central.or g
    (remove eights to email me)

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Mick White

    #2
    Re: Javascript --- synchronizing two select lists

    Weston C wrote:[color=blue]
    > I'm trying to keep the selected / displayed option of two different
    > select lists synchronized -- so when the user selects, say, the third
    > option on one of them, the third option on the other is displayed:
    >
    > http://www.managerassistant.com/newpurchase.html
    >
    > Here's the code I'm trying -- but that doesn't seem to work:
    >
    > function synchronizeSele cts(thisSelect, otherSelect)
    > {
    > // find the selected option in thisSelect[/color]
    ....
    function synchronizeSele cts(thisSelect, otherSelect){
    otherSelect.sel ectedIndex= thisSelect.sele ctedIndex;
    }

    <select name="product" id="prod" class="smallsel ects"
    onchange="synch ronizeSelects(t his,this.form.s upp)">

    <select name="supp" id="supp" class="smallsel ects"
    onchange="synch ronizeSelects(t his,this.form.p roduct)">

    Mick

    Comment

    • asmo

      #3
      Re: Javascript --- synchronizing two select lists

      Try this:

      <script type="text/javascript">
      function syncSelect( first, second ) {
      var first = (typeof first == "string") ? document.getEle mentById(
      first ) : first;
      var second = (typeof second == "string") ? document.getEle mentById(
      second ) : second;
      second.selected Index = first.selectedI ndex;
      }
      </script>

      <select id="one" onchange="syncS elect(this, 'two')">
      <option>one</option>
      <option>two</option>
      <option>three </option>
      </select>

      <select id="two" onchange="syncS elect(this, 'one')">
      <option>one</option>
      <option>two</option>
      <option>three </option>
      </select>

      should work..

      Regards
      Jan

      Comment

      Working...