Solved

How to sort the select box contents ?

Posted on 2001-06-23
2
170 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

932 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now