Tim Habersack

Where I put my things..

Moving options between select boxes

Feb 8th 2013

Just a quick post. I needed to be able to display a multi-select box showing all the options available to a user, and allow the user to select one or several, and 'move' them to their list of chosen options. Then, when the form is submitted, make sure only the items in the chosen list of options is submitted.

Fairly easy right? It was, but took long enough that I wanted to share and tag appropriately, in case people come looking for it. :)

Image of what I wanted: Multiselect image

Link to my working JSFiddle.

HTML:


Javascript:

$('.go_in').click(function() {
    return !$('.all_options option:selected').remove().appendTo('.chosen_options');
});
$('.go_out').click(function() {
   return !$('.chosen_options option:selected').remove().appendTo('.all_options'); 
});

$('form').submit(function() {
    $('.all_options option').prop('selected','');
    $('.chosen_options option').prop('selected','selected');
    alert($(this).serialize());
});