0

I am new to jQuery so this is probably very simple but I can't seem to find an example with using sortable with Select Options. Can it be done?

$(document).ready(function(){ 
    $("#list").sortable({})
});

        <select id='list' size='8'>
            <option value="0">Option 0</option>
            <option value="1">Option 1</option>
            <option value="2">Option 2</option>
        </select>

I'm able to get sortable to work with other elements, but it does not with the Select

mseifert
  • 5,390
  • 9
  • 38
  • 100
  • why do you need to do this? cant you refactor the select element to a list item so it conforms to the sortable params – hammus Nov 20 '13 at 02:40
  • I already have the list and it needs to be an option list as part of a form. I am trying to avoid having to add another visual element when the list is already there. I guess what I am checking is that jquery sortable isn't designed for options, which appears to be the case. – mseifert Nov 20 '13 at 02:45

1 Answers1

0

Try this

The solution

$.fn.sortOptions = function(){
    $(this).each(function(){
        var op = $(this).children("option");
        op.sort(function(a, b) {
            return a.text > b.text ? 1 : -1;
        })
        return $(this).empty().append(op);
    });
}

Usage:

$("select").sortOptions();

Refer this

Community
  • 1
  • 1
vijaykumar
  • 4,658
  • 6
  • 37
  • 54