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.
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).
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.