DelphiFAQ Home Search:

How to insert a string into a sorted SELECT listbox


comments43 comments. Current rating: 3 stars (34 votes). Leave comments and/ or rate it.


I have a sorted listbox and want to insert a new string without a complete resort.


Use the javascript function shown below - pass the string that you want to insert as an argument. The name of the SELECT listbox is hard-coded in the script as 'mylist'.

Call it like this:
insertSortedLb('New Name')

The case is case sensitive and sorts alphabetically.
That means if you have numbers in the listbox, and these numbers include
10, 2, 300
then it will sort these numbers as
10, 2, 300
NOT as
2, 10, 300

function insertSortedLb(sVal) {
var lb = document.getElementById('mylist');

oOption = document.createElement("OPTION");
oOption.text = '';
oOption.value = '';
oOption.selected = false;
lb.options[lb.length] = oOption;

for(i=lb.length-1; i>0; i--)  {
  if (lb.options[i-1].text < sVal) {
    oOption = document.createElement("OPTION");
    oOption.text = sVal;
    oOption.value = sVal;
    oOption.selected = false;
    lb.options[i] = oOption;
  else {
    lb.options[i].text = lb.options[i-1].text;
    lb.options[i].value = lb.options[i-1].value;
} // for
} // end of insertSortedLb()

Content-type: text/html


You are on page 2 of 3, other pages: 1 [2] 3
2013-04-06, 12:17:00
bypkhjpb payday loans rGeoVF payday advance aHVnST Quick Quid 1435 payday loans 4390
2013-04-06, 23:29:27
coxrrrw where to buy viagra rIoNA cialis 7913
2013-04-08, 01:20:07
lebvirri no prescription order viagra online :-O cialis for order from canada >:-OOO
2013-04-10, 05:14:23
hzclyzpf instant payday loans 3743 payday loans australia FZCRW pay day loan Kjhqf pay day loans bKynq
2013-04-10, 09:39:13
gxpwixdh cheap viagra >:-OOO cialis cheap OOjYC
2013-04-11, 09:33:06
xgonoyc payday loans yybCnL pay day loan nKyOh Bad Credit Loans 2633 payday loans online %-[[[
2013-04-15, 16:39:07
jqezvys payday loans online %-[[[ payday loan poeAV payday loans XtyesA online payday 9885
2013-04-30, 10:26:40
upfnuzrx fast payday loan yEUHzS payday loan Canada >:]] same day loan :-O payday loans rsCnke
2013-05-01, 09:33:16
rrszbqs payday loans online OzeXB payday advance pGDEr Payday advance >:-OOO payday loans Canada 0971
2013-05-06, 09:08:35
kyqruh fast payday loan GgeFZ payday loan Alberta aHSSx payday advances 5860 payday loans online KjaFYT
2013-05-08, 10:42:11
nsheei Bad Credit Loans =-] payday loans PzEbsd cash advance aZRPgt Cash Advance 4070
2013-06-08, 07:37:18
same day loan  
ninuowv same day loan moGlGy payday loan 7563 payday loan Canada vIIru
2013-06-19, 00:39:08
tuzacg payday advances ClmYlh no fax payday loans wqxyQk cash advances 9526
2013-06-22, 02:45:05
anonymous from India  
It's all very cool. Juicy and very badly written! I love these unusual items! thanks a lot!!! http://howtoreduceh..naturally/
2013-06-25, 03:38:28
anonymous from India  
i will try this. thank you.
You are on page 2 of 3, other pages: 1 [2] 3



NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: