Whether you are working with select boxes that are generated outside of your control, or trying to implement sorting features in your user interface, you may find it necessary to dynamically reverse the options in a select box.
These select box options are reversed dynamically here with Javascript
The Javascript:
function reverse_options(select){ select_box = document.getElementById(select); options = select_box.options; var text_array = new Array(); var value_array = new Array(); var len = options.length; for(var i=0; i
The HTML
First we get the select box through the DOM and then get an array that contains it's options. Next we iterate through that array and create our own arrays contains the text and values of the options. Finally, we use Javascript's reverse function to reverse those arrays, and write them back into the select box.





