Solved

How to sort the select box contents ?

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sorting a string 1 29
How to generate a JSON response in coldfusion 4 41
Using javascript confirmation for a delete in mysql 5 31
How to show span when clicked on? 10 21
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

770 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