3

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.

3 Answers 3

2

Call the following function each time an option moves to rearrange the select options from #country_select alphabetically

Js:

function reArrangeSelect() {
    $("#country_select").html($("#country_select option").sort(function(a, b) {
        return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
    }))
}

Live Demo | Source | Credit

Sign up to request clarification or add additional context in comments.

Comments

1

Custom element sorter based on values

function optionSort(a, b) {
    return $(a).val() > $(b).val();
}

var opts = $('#selectID option').get();

$('#selectID ').html(opts.sort(optionSort))

DEMO : http://jsfiddle.net/5WgYK/

Comments

0

If you need to sort the multiselect by selected whilst keeping the selected items at the top of the list, this will be useful.

$('#identifier option:selected').prependTo('#identifier');
$("#identifier").multiselect("refresh");  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.