I am using the following code for adding country where user selects country from one mutiple-select and adds to another multiple-select and on submit the countries of second multiple-select is inserted into database.
.js
$(document).ready(function(){
$('.add').click(function(){
$('#country_list option:selected').appendTo('#country_select');
});
$('.remove').click(function(){
$('#country_select option:selected').appendTo('#country_list');
});
$('.add-all').click(function(){
$('#country_list option').appendTo('#country_select');
});
$('.remove-all').click(function(){
$('#country_select option').appendTo('#country_list');
});
$("#register").click(function() {
$("#country_select option").each(function() {
$(this).attr("selected", "selected");
});
});
});
.html
<select id="country_list" size="10" multiple style="min-width: 200px;">
<option value="US">United States</option>
<option value="UK">United Kingdom</option>
<option value="IN">India</option>
</select>
<table border="0">
<tr><td><input type="button" class='add' value=">"/></td></tr>
<tr><td><input type="button" class='remove' value="<"/></td></tr>
<tr><td><input type="button" class='add-all' value=">>>"/></td></tr>
<tr><td><input type="button" class='remove-all' value="<<<"/></td></tr>
</table>
<select size="10" name="country_select[]" id="country_select" multiple style="min-width: 200px;">
</select>
Here when add button is clicked the selected items in dropdown country-list is moved to country-select.
What I require is that when a country is added then it should be moved into the dropdown country-select in order of name. Currently when a country is added it goes to the bottom of the list in country-options.
I tried this Javascript to sort contents of select element but not working.