Jump to content

Alphabetized Dropdown Menu


fazlionline

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...