Alex Rivera | Logout

Sorting dropdown list using Javascript

Asked 2009-03-20T16:53:13.723
26

i want to sort the drop down items using javascript,can anyone tell me how to do this.

Edit
Report

1 Answer

57

You could use jQuery and something like this:

$("#id").html($("#id option").sort(function (a, b) {
    return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
}))

But it's probably better to ask why and what you're trying to accomplish.

answered 2009-03-20T17:41:59.363

Your Answer