Looking for beta testers for WhizzyInvoice.com. Wanna help? Jump to: main content, navigation or the bottom.

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 the example Javascript: Swapping select box options now!


Comments (1)

What others have said about this post.

  • About 3 years ago Miguel Angel Sanchez said:

    Excellent. Brief Clear Functional

    Last Edit: February 12, 2010, 20:05:01 by qodo  

Got something to say?

Join the discussion! You know how these things work; enter your details and comments below and be heard.

If you have trouble reading the code, click on the code itself to generate a new random code.
 

Going up? Back to the top