Solved

How to sort the select box contents ?

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

758 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

17 Experts available now in Live!

Get 1:1 Help Now