
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:
thanks. this saved me 30 minutes of work.
This was fantastic help. Now on to the next challenge.
Really good one. Somebody needs to take initiative for repeatative work..And that you
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]);
Thanks so much for this code..saved a lot of time
Very simple and effective code..thanks
works like a charm and saved me a bunch of time - thanks!!!! ;)
are very cool code man
really.........
helped a lot
ha ha ha ha ha ha ha ha ha ha ha ha
***************************************8
Tks for the example, very clear and didatic!
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!
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
Very useful functions, thanks.
Post a Comment