views:

67

answers:

1

Hi all

Below is my HTML

<select name="name" id="id">
    <option value="1">ZZZ</option>
    <option value="1">PPP</option>
    <option value="1">SSS</option>
    <option value="1">AAA</option>
</select>

I want that this select box will be sorted according to below.

<select name="name" id="id">
        <option value="1">AAA</option>
        <option value="1">PPP</option>
        <option value="1">SSS</option>
        <option value="1">ZZZ</option>
</select>

I am using mootools1.2 as my JS frame work.

Thanks
Avinash

+1  A: 

Maybe there's a better way to do this, but the following works;

var values = [];
$('selectList').getElements('option').each(function(elem) {
    values.push(elem.get('text'));
});

values.sort();

$('selectList').empty();
$each(values, function(value) {
    new Element('option')
        .set('text', value)
        .inject($('selectList'));
});


<form>
    <select id="selectList">
        <option>b</option>
        <option>c</option>
        <option>a</option>
    </select>
</form>
Björn
thanks its works for me fine.....
Avinash