select boxes and ordering

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Thies

    select boxes and ordering

    I have 10 select boxes, they have select values of 1 to 10 (one of
    each value).

    I'd like to be able to change one of the select boxes and have the
    rest reorder themselves accordingly.

    I thought this would be simple, as the values of the other select
    boxes are either incremented up (if going from a smaller value to a
    larger) or incremented down over the range of the old value to (and
    including) the new value.

    Then it occured to me that when you do an onchange that you only
    know the new value, not the old.

    Anyone have a simple solution to this? I think I must just be
    missing something.

    Cheers,
    Jeff
    (at the girlfriends, sorry about the Google post)
  • Jeff Thies

    #2
    Re: select boxes and ordering

    > I have 10 select boxes, they have select values of 1 to 10 (one of[color=blue]
    > each value).
    >
    > I'd like to be able to change one of the select boxes and have the
    > rest reorder themselves accordingly.
    >
    > I thought this would be simple, as the values of the other select
    > boxes are either incremented up (if going from a smaller value to a
    > larger) or incremented down over the range of the old value to (and
    > including) the new value.
    >
    > Then it occured to me that when you do an onchange that you only
    > know the new value, not the old.[/color]

    Here's what I came up with:

    var orderArray=new Array(); // hash to store last values.
    function setOrderArray() { // call this function on initialize
    var form=document.f orms[0];
    for (var i=0; i<form.length;i ++){
    var el=form[i];
    if(el.type=='se lect-one'){ // assumes only rolldowns are for "ordering"
    orderArray[el.name]=(el.selectedIn dex +1);
    }
    }
    }

    function setOrder(obj){ // call this onchange="setOr der(this)" in the //
    rolldowns
    var to=obj.selected Index+1;
    var from=orderArray[obj.name];
    for (var elname in orderArray){
    var n=orderArray[elname];
    if((from > to)&&(n >= to)&&(n < from)){
    document.forms[0][elname].selectedIndex= document.forms[0][elname].selectedIndex+ 1;
    }
    if((from < to)&&(n <= to)&&(n > from)){
    document.forms[0][elname].selectedIndex= document.forms[0][elname].selectedIndex-1;
    }
    }
    setOrderArray() ;
    }

    A bit funky, but works...

    Cheers,
    Jeff
    [color=blue]
    >
    > Anyone have a simple solution to this? I think I must just be
    > missing something.
    >
    > Cheers,
    > Jeff
    > (at the girlfriends, sorry about the Google post)[/color]

    Comment

    Working...