Solved

How to sort the select box contents ?

Posted on 2001-06-23
2
172 Views
Last Modified: 2008-03-04
How to sort the select box contents ?
0
Comment
Question by:qetuo08
  • 2
2 Comments
 
LVL 12

Accepted Solution

by:
ahosang earned 30 total points
ID: 6222032
Alphabetically?

<html>
<head>
<script>
function sortSelect(selectBoxObj) {
  arr=new Array();
  for(i=0;i<selectBoxObj.length;i++) {
    arr[i]=new Array();
    arr[i][0]=selectBoxObj.options[i].text;
    arr[i][1]=selectBoxObj.options[i].value;
  }
  arr.sort();
  selectBoxObj.length=0;
  for (j=0;j<arr.length;j++) {
    selectBoxObj.options[j]=new Option(arr[j][0], arr[j][1]);
  }
}

</script>
<body>

<form action="">
<select name="sel">
    <option value="1">d
    <option value="2">c
     <option value="2">a
     <option value="2">b
</select>
<input type="button" value="Sort" onclick="sortSelect(this.form.sel)">
</form>

</body>
</html>
0
 
LVL 12

Expert Comment

by:ahosang
ID: 6222198
Or numerically?
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question