Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting dropdown list using Javascript

Tags:

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

like image 967
Chetan Avatar asked Mar 20 '09 16:03

Chetan


People also ask

How do I sort by dropdown value?

We use the following valid_if formulas to sort the values that appear in the dropdown menus: 1. ORDERBY(Person[Key], [First Name]) 2. ORDERBY(Person[Key], [Last Name]) 3. ORDERBY(Person[Key], [First Name], TRUE) 4.

What is drop down list in Javascript?

A dropdown list is a toggleable menu that allows the user to choose one option from multiple ones. The options in this list are defined in coding, which is associated with a function. When you click or choose this option, that function triggers and starts performing.


1 Answers

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.

like image 158
masto Avatar answered Sep 29 '22 14:09

masto