Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you sort an <option> list in JavaScript? 1

Status
Not open for further replies.

JohnnyBGoode

Programmer
May 1, 2002
76
0
0
CA
I currently have a select list:

Code:
<select name="list">
  <option value="a">A</option>
  <option value="b">B</option>
  <option value="c">C</option>
</select>

I add and remove items dynamically using the folowing:
Code:
list.options.add(oOptionItem);
list.options.remove(i);

However, when I add and remove, it does so at the end of the list (eg. remove "A", then re-add "A", it is added at the bottom). How can I easily re-sort this list (alpha)? Is there a javascript function for this?

Thank you.
 
Did you check the FAQs for this forum? Dan wrote a FAQ on this very subject over 2 years ago:

faq216-5347

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
You could put all the values into an array then sort the array and write the values back out to the list.



At my age I still learn something new every day, but I forget two others.
 
Beauty! I looked at previous posts, but didn't search the FAQ. Kaht....that worked perfectly. Thx, bud.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top