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.

Question:

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

Answer:

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;
    break;
  }
  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

Comments:

You are on page 2 of 3, other pages: 1 [2] 3
2013-04-06, 12:17:00
 
rating
bypkhjpb http://paydayloansa..iadsb.com/ payday loans rGeoVF http://paydayloansusadsb.com/ payday advance aHVnST http://paydayloansu..dsb.co.uk/ Quick Quid 1435 http://paydayloansc..adadsb.ca/ payday loans 4390
2013-04-06, 23:29:27
 
rating
coxrrrw http://buyviagrafsc.com/ where to buy viagra rIoNA http://buycialisfsc.com/ cialis 7913
2013-04-08, 01:20:07
 
rating
lebvirri http://orderviagradsb.com/ no prescription order viagra online :-O http://ordercialisdsb.com/ cialis for order from canada >:-OOO
2013-04-10, 05:14:23
 
rating
hzclyzpf http://paydayloansusadsa.com/ instant payday loans 3743 http://paydayloansa..iadsa.com/ payday loans australia FZCRW http://paydayloansc..adadsa.ca/ pay day loan Kjhqf http://paydayloansu..dsa.co.uk/ pay day loans bKynq
2013-04-10, 09:39:13
 
rating
gxpwixdh http://cheapviagrafsc.com/ cheap viagra >:-OOO http://cheapcialisfsc.com/ cialis cheap OOjYC
2013-04-11, 09:33:06
 
rating
xgonoyc http://paydayloansa..iadsc.com/ payday loans yybCnL http://paydayloansusadsc.com/ pay day loan nKyOh http://paydayloansu..dsc.co.uk/ Bad Credit Loans 2633 http://paydayloansc..adadsc.ca/ payday loans online %-[[[
2013-04-15, 16:39:07
 
rating
jqezvys http://paydayloansusadsd.com/ payday loans online %-[[[ http://paydayloansa..iadsd.com/ payday loan poeAV http://paydayloansc..adadsd.ca/ payday loans XtyesA http://paydayloansu..dsd.co.uk/ online payday 9885
2013-04-30, 10:26:40
 
rating
upfnuzrx http://paydayloansusapwc.com/ fast payday loan yEUHzS http://paydayloansusapwc.ca/ payday loan Canada >:]] http://paydayloansu..pwc.co.uk/ same day loan :-O http://paydayloansa..iapwc.com/ payday loans rsCnke
2013-05-01, 09:33:16
 
rating
rrszbqs http://paydayloansa..iapwd.com/ payday loans online OzeXB http://paydayloansusapwd.com/ payday advance pGDEr http://paydayloansu..pwd.co.uk/ Payday advance >:-OOO http://paydayloansusapwd.ca/ payday loans Canada 0971
2013-05-06, 09:08:35
 
rating
kyqruh http://paydayloansusapwi.com/ fast payday loan GgeFZ http://paydayloansusapwi.ca/ payday loan Alberta aHSSx http://paydayloansu..pwi.co.uk/ payday advances 5860 http://paydayloansa..iapwi.com/ payday loans online KjaFYT
2013-05-08, 10:42:11
 
rating
nsheei http://paydayloansu..pwj.co.uk/ Bad Credit Loans =-] http://paydayloansa..iapwj.com/ payday loans PzEbsd http://paydayloansusapwj.com/ cash advance aZRPgt http://paydayloansusapwj.ca/ Cash Advance 4070
2013-06-08, 07:37:18
same day loan  
rating
ninuowv http://paydayloansu..cxc.co.uk/ same day loan moGlGy http://paydayloansusacxc.com/ payday loan 7563 http://paydayloansc..adacxc.ca/ payday loan Canada vIIru
2013-06-19, 00:39:08
 
rating
tuzacg http://paydayloansu..ala.co.uk/ payday advances ClmYlh http://paydayloansusaala.com/ no fax payday loans wqxyQk http://paydayloansc..adaala.ca/ 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. http://besttechschools.org
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: