Jump to content

Alphabetized Dropdown Menu


Recommended Posts

There is nothing built-in to html that will sort your list, it would be best to generate your options list dynamically on the server (Server scripting)You can do it with Javascript too, here is a method to do it :

<html> <body> <form action=""> <div>   <select name="select"> 	<option>test</option> 	<option>aaa</option> 	<option>ccc</option> 	<option>ddd</option> 	<option>123</option>   </select> </div> </form> <script type="text/javascript"> window.onload = function() {   // Sort ALL select elements   var selectArr = document.getElementsByTagName('select');   for (var i = 0; i < selectArr.length; i++) { 	var oArr = []; 	// Get the options for the select element 	for (var j = 0; j < selectArr[i].options.length; j++) { 	  // Store this as an object that has an option object member, and 	  // a toString function (which will be used for sorting) 	  oArr[oArr.length] = { 		option : selectArr[i].options[j], 		toString : function() { 		  // Return the text of the option, not the value 		  return this.option.text; 		} 	  } 	} 	// Sort the array of options for this select 	oArr.sort(); 	// Remove all options from the select 	selectArr[i].options.length = 0; 	// Rebuild the select using our sorted array 	for (var j = 0; j < oArr.length; j++) { 	  selectArr[i].options[j] = oArr[j].option; 	}   } }; </script> </body> </html>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...