Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dropdown sorting alphabetically

Status
Not open for further replies.

fisa

Programmer
Nov 28, 2003
19
0
0
GB
Friends,

I am quite new to Javascripts. I need to sort a dropdown on my html page in alphabetical order, before it is loaded. The drop down has static elements in it.

Could anyone help me out in getting this done. Thanks in advance.

Fisa
 
You can't sort it before it has loaded - if it hasn't loaded, there will be nothing to sort. You will have to do this after it has loaded (client-side), or sort the options before delivering them to the page (server-side).

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
If the values in the dropdown are provided from a database then you are using some server-side code to get them so just use that code to sort the data before it writes it into the dropdown.

If you want to use javascript to do it client-side then your best bet is to put all of the options into a javascript array, use javascripts ability to sort an array then write the data out to the dropdown box either dynamically building the dropdown as the page loads or executing a function to populate the dropdown with an onload event.

If you do it client-side you have to know that your clients will not have javascript disabled though or you will not get the options in the dropdown.



Stamp out, eliminate and abolish redundancy!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top