Click here to Skip to main content
15,885,897 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
JavaScript
var list =["haider","faisal","adnan","kami","asad"]
var handles  = list.sort();
var options = '';
        for (var i = 0; i < handles.length; i ++) {
            options += '<option value="' + handles[i] + '">' + handles[i] + '</option>';
        }
        $('#listBox').html(options);

now i want to insert "faizan" and the list order is maintained any idea how to do it?
Posted
Updated 23-Jun-12 19:59pm
v8
Comments
Sergey Alexandrovich Kryukov 23-Jun-12 16:27pm    
What do you mean "maintained"? If you mean alphanumeric order, it is already not maintained, even before Asad is added...
--SA
agha_ali22 24-Jun-12 1:46am    
var handles = list.sort() is sorting the array

El-salmo 3likom wa ra7mat ALLAH we Barakatoh,

Dear,

another Solution:
you can add "faizan" to the list
JavaScript
var list =["haider","faisal","adnan","kami","asad"]
function addOption(Obj)
{
//clear list
$('#listBox').html("");
//add new option
list.push(Obj);
//sort the array
var handles  = list.sort();
var options = '';
        for (var i = 0; i < handles.length; i ++) {
         options += '<option value="' + handles[i] + '">' + handles[i] + '</option>';
        }
        $('#listBox').html(options);

all you have to do :
call the function --> addOption("faizan");

Best Regards:
Ahmed Assaf
eBusiness Developer
 
Share this answer
 
Comments
agha_ali22 25-Jun-12 5:06am    
thanks
JavaScript
$('#listBox').append("<option value="faizan">faizan</option>");
 
Share this answer
 
Comments
agha_ali22 24-Jun-12 1:47am    
it will append the faizan in the last i want to append without disturbing its order
mhamad zarif 24-Jun-12 5:04am    
Do u mean that u want to insert it at the beginning ?
agha_ali22 24-Jun-12 7:50am    
see my solution
just write this after your code
$("#listBox").sortable('refresh');
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900