Wednesday, October 04, 2006

Move options up and down select lists


In my previous post I showed how to move options in a select list from one list to another with JavaScript. Once you've moved them, you may want to reorder them if order is important. So, now I'll show how to do that.

First, define your control in HTML to look something like what is shown to the right where you have up and down buttons which will be used to move selected elements up and down in the list.

Your HTML will look something like:

<select id="orderedList" multiple="multiple"></select>
<img src="moveup.gif" alt="Move Up" onclick="moveOptionsUp('orderedList')" />
<img src="movedown.gif" alt="Move Down" onclick="moveOptionsDown('orderedList')" />

As you can see, I've added two image which when you click on them invoke the moveOptionsUp and moveOptionsDown function. The JavaScript for these functions looks like:

// moveOptionsUp
//
// move the selected options up one location in the select list
//
function moveOptionsUp(selectId) {
var selectList = document.getElementById(selectId);
var selectOptions = selectList.getElementsByTagName('option');
for (var i = 1; i < selectOptions.length; i++) {
var opt = selectOptions[i];
if (opt.selected) {
selectList.removeChild(opt);
selectList.insertBefore(opt, selectOptions[i - 1]);
}
}
}

and

// moveOptionsDown
//
// move the selected options down one location in the select list
//
function moveOptionsDown(selectId) {
var selectList = document.getElementById(selectId);
var selectOptions = selectList.getElementsByTagName('option');
for (var i = selectOptions.length - 2; i >= 0; i--) {
var opt = selectOptions[i];
if (opt.selected) {
var nextOpt = selectOptions[i + 1];
opt = selectList.removeChild(opt);
nextOpt = selectList.replaceChild(opt, nextOpt);
selectList.insertBefore(nextOpt, opt);
}
}
}

Unfortunately there's no insertAfter function for a node, so the moveOptionsDown function is a little funky.

12 comments:

Anonymous said...

thanks. this saved me 30 minutes of work.

Anonymous said...

This was fantastic help. Now on to the next challenge.

Anonymous said...

Really good one. Somebody needs to take initiative for repeatative work..And that you

Anonymous said...

There is no insertAfter function but you can make the following code in moveOptionsDown function :

var nextOpt = selectOptions[i + 1]; selectList.removeChild(nextOpt );
selectList.insertBefore(nextOpt , selectOptions[i]);

Anonymous said...

Thanks so much for this code..saved a lot of time

smith said...

Very simple and effective code..thanks

Anonymous said...

works like a charm and saved me a bunch of time - thanks!!!! ;)

Anonymous said...

are very cool code man
really.........
helped a lot


ha ha ha ha ha ha ha ha ha ha ha ha
***************************************8

Foxmcloud said...

Tks for the example, very clear and didatic!

Anonymous said...

Saved loads of dev time. Thanks. Slight bug in ie7 - the selected item won't move to the top position of the list. Solved by adding orderedList.focus(); after calling the moveup function on the button. Cool!

Anonymous said...

I had found another way of doing this but this was much simpler and meant I code cut 10 lines of PHP code out reformatting the contents of a field.

Many thanks

Anonymous said...

Very useful functions, thanks.