Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reorder the options in a SELECT list with jQuery

Tags:

html

jquery

Any suggestion how to resort/reorder the <option> elements inside that <select> ? Here is the html:

<select class="required" name="jform[params][language]" id="jform_params_language">
    <option value="cs-CZ">Czech (Cestina)</option>
    <option value="en-GB">English (United Kingdom)</option>
    <option value="sk-SK">Slovak (Slovencina)</option>
</select>

I need it to be in this way:

<select class="required" name="jform[params][language]" id="jform_params_language">
    <option value="sk-SK">Slovak (Slovencina)</option>
    <option value="en-GB">English (United Kingdom)</option>
    <option value="cs-CZ">Czech (Cestina)</option>
</select>

Need to mention that i can't modify the html. I tried to use jQuery's sort() function but do not know how to do it.

like image 600
aspirinemaga Avatar asked Apr 14 '13 00:04

aspirinemaga


1 Answers

You can use reverse method.

var $sel = $('#jform_params_language'),
    $o = $sel.children().toArray().reverse();

$sel.append($o); //[0].selectedIndex = 0;

http://jsfiddle.net/3UWx6/

like image 199
undefined Avatar answered Nov 15 '22 01:11

undefined