select boxes and ordering

J

Jeff Thies

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)
 
J

Jeff Thies

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.

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.forms[0];
for (var i=0; i<form.length;i++){
var el=form;
if(el.type=='select-one'){ // assumes only rolldowns are for "ordering"
orderArray[el.name]=(el.selectedIndex +1);
}
}
}

function setOrder(obj){ // call this onchange="setOrder(this)" in the //
rolldowns
var to=obj.selectedIndex+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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,085
Messages
2,570,597
Members
47,218
Latest member
GracieDebo

Latest Threads

Top