Ah, but be careful
The important thing to remember about using this type of function is that you have to select all of the options in the select box before submitting for them to be sent to your server-side page. This type of script can pose accessibility problems as the normal behaviour of a select box is changed.
function swapSelectOptions(selectFrom,selectTo,swapAll) {
if (typeof(selectFrom) == "string") {
availableitems = document.getElementById(selectFrom);
}
if (typeof(selectTo) == "string") {
selecteditems= document.getElementById(selectTo);
}
for (var i = 0; i < availableitems.length; i++) {
if (availableitems.options[i].selected || swapAll) {
selecteditems.options[selecteditems.options.length] = new Option(availableitems.options[i].text);
selecteditems.options[selecteditems.options.length-1].value = availableitems.options[i].value;
availableitems.options[i].selected = false; availableitems.options[i] = null; i--;
}
}
}
Download
Download the example Javascript: Swapping select box options now!
Got something to say?
Join the discussion! You know how these things work; enter your details and comments below and be heard.
Comments 1