Xaisoft
Xaisoft

Reputation: 46591

How would you dynamically order an <option select> list using JQuery?

If I have a dropdown and a listbox, is there a way to order the listbox based on the dropdown using JQuery? An example would be helpful.

Upvotes: 1

Views: 8295

Answers (1)

Seb
Seb

Reputation: 25147

This alters the order in the pulldown. You'll have to set the order depending on your own criteria:

<select id="the-select">
    <option value="1">First option</option>
    <option value="2">Second option</option>
</select>

<script type="text/javascript">
//<![CDATA[
    $(function (){
        $("#the-select option[value=2]").insertBefore("#the-select option[value=1]");
    });
//]]>
</script>

Upvotes: 2

Related Questions