is there a quick way to sort the items of a select element? Or I have to resort to writing javascript?
Please any ideas.
JavaScript
x
6
1
<select size="4" name="lstALL" multiple="multiple" id="lstALL" tabindex="12" style="font-size:XX-Small;height:95%;width:100%;">
2
<option value="0"> XXX</option>
3
<option value="1203">ABC</option>
4
<option value="1013">MMM</option>
5
</select>
6
Advertisement
Answer
This will do the trick. Just pass it your select element a la: document.getElementById('lstALL')
when you need your list sorted.
JavaScript
1
18
18
1
function sortSelect(selElem) {
2
var tmpAry = new Array();
3
for (var i=0;i<selElem.options.length;i++) {
4
tmpAry[i] = new Array();
5
tmpAry[i][0] = selElem.options[i].text;
6
tmpAry[i][1] = selElem.options[i].value;
7
}
8
tmpAry.sort();
9
while (selElem.options.length > 0) {
10
selElem.options[0] = null;
11
}
12
for (var i=0;i<tmpAry.length;i++) {
13
var op = new Option(tmpAry[i][0], tmpAry[i][1]);
14
selElem.options[i] = op;
15
}
16
return;
17
}
18