/    Sign up×
Community /Pin to ProfileBookmark

Change indexes in a select box

I have a select box full of options. I want users to be able to rearrage the order of the options listed by selecting an option and then clicking a “Move Up” or Move Down” link that sends the name of the list box and a “+1” for down and “-1” for up to the javascript function below. On the screen it works fine, but my issue is that when the form is submitted the options in the box are sent in the original order they were in before the user changed it even though they are different on the screen. What do I need to do to fix the code.

[CODE]function MoveUpDown(dest,to) {
index = dest.selectedIndex;
var list = dest;
var total = list.options.length-1;
if (index == -1) return false;
if (to == +1 && index == total) return false;
if (to == -1 && index == 0) return false;
var items = new Array;
var values = new Array;
for (i = total; i >= 0; i–) {
items[i] = list.options[i].text;
values[i] = list.options[i].value;
}
for (i = total; i >= 0; i–) {
if (index == i) {
list.options[i + to] = new Option(items[i],values[i + to], 0, 1);
list.options[i] = new Option(items[i + to], values[i]);
i–;
}
else {
list.options[i] = new Option(items[i], values[i]);
}
}
list.focus();
}
[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJun 17.2005 — I'm using the following code for my shift up's and shift down's. I just checked, and my options submit correctly after shifting -- i.e., in the order of their new positions.
function shiftUp(s) {
o = s.options;
for (var i=1; i<o.length; i++) {
if (o[i].selected && o[i].value != "00") {
saveopt1 = new Array(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
saveopt2 = new Array(o[i-1].text, o[i-1].value, o[i-1].defaultSelected, o[i-1].selected);
o[i] = new Option(saveopt2[0], saveopt2[1], saveopt2[2], saveopt2[3]);
o[i-1] = new Option(saveopt1[0], saveopt1[1], saveopt1[2], saveopt1[3]);
o[i].selected = saveopt2[3];
o[i-1].selected = saveopt1[3];
}
}
}
function shiftDown(s) {
o = s.options;
for (var i=(o.length-2); i>=0; i--) {
if (o[i].selected && o[i].value != "00") {
saveopt1 = new Array(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
saveopt2 = new Array(o[i+1].text, o[i+1].value, o[i+1].defaultSelected, o[i+1].selected);
o[i] = new Option(saveopt2[0], saveopt2[1], saveopt2[2], saveopt2[3]);
o[i+1] = new Option(saveopt1[0], saveopt1[1], saveopt1[2], saveopt1[3]);
o[i].selected = saveopt2[3];
o[i+1].selected = saveopt1[3];
}
}
}
Copy linkTweet thisAlerts:
@mitchell8380authorJun 17.2005 — Thank You! that works perfectly!
Copy linkTweet thisAlerts:
@phpnoviceJun 17.2005 — Cheers.
×

Success!

Help @mitchell8380 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...