By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,309 Members | 1,452 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,309 IT Pros & Developers. It's quick & easy.

select boxes and ordering

P: n/a
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)
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
> 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[i];
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

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)

Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.