Add/Remove Selected Element from MultiSelect

In this tutorial I will provide you script that will use to swapping element between two multi select. This is very common functionality in web development.Multi select is very useful when you are selecting multiple element. Its same as select you can transform normal select to multi using multiple="true" property into normal select. We are using jQuery to move element from one select to another same for reverse.

Checkout other tutorial of jQuery,

Simple Example move Element Between two Multi Select Using jQuery

Step 1: We will create two element and added button between them.

<div class="row text-center">


<div class="col-xs-4">
            <select class="form-control" id="features" name="Features[]" multiple="multiple" style="height:200px;">


<option value="2">Row 2</option>  


<option value="4">Row 4</option>  


<option value="5">Row 5</option>  


<option value="6">Row 6</option>  


<option value="7">Row 7</option>  


<option value="8">Row 8</option>  


<option value="9">Row 9</option> 


</select>
</div>




<div class="col-xs-2">


<div class="input-group">
                <button type="button" value="" class="btn btn-xs btn-primary" id="add">Add >></button>
</div>




<div class="input-group" style="padding-top:10px;">
				<button type="button" value="" class="btn btn-xs btn-danger" id="remove"><< Remove</button> 
</div>


</div>




<div class="col-xs-4">
            <select class="form-control" name="FeatureCodes[]" size="9" id="selected_features" multiple="multiple" style="height:200px;">


<option value="1">Row 1</option>  


<option value="3">Row 3</option>


</select>
</div>


</div>


Step 2 : We will add jQuery code which is used for move element between them.

$(document).ready(function(){
   $('#add').click(function() {  
    return !$('#features option:selected')
.remove().appendTo('#selected_features');  
   });  
   $('#remove').click(function() {  
    return !$('#selected_features option:selected')
.remove().appendTo('#features');  
   });  
    
function selectall()  {  
$('#selected_features').find('option').each(function() {  
   $(this).attr('selected', 'selected');  
  });  
}  
});  

Result View

moving element multiselect

Demo & Download Source Code

Please feel free to send queries to me using below comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *