I have select
<select multiple id="select2">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
And two buttons
<input type="button" value="Up" onclick="up()">
<input type="button" value="Down" onclick="down()">
How can I move selected option in multiselect up and down by buttons using jquery?
if you do not use jquery
Update: Fixed code for multiple options selected, based on @patrick dw's suggestion.
Test it here »
No need to use inline
onclick=""
event listeners. jQuery takes full control of separating presentation from functionality.I created a jquery plugin for this: https://github.com/UziTech/jquery.moveSelected.js
usage:
http://jsfiddle.net/UziTech/qr5qfhgg/
here is the same idea as the previously-posted non-jquery example, but with some strategic code re-use. My need was to have the buttons always operate on the same select element, named "cols". You could put "sel" as a parameter of the moveUp() and moveDown() functions if you want something more generic.