[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to sort the select box contents ?

Posted on 2001-06-23
2
Medium Priority
?
179 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 120 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…
Suggested Courses

830 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