...

View Full Version : need help with select multiple



newbie2006
07-28-2006, 02:16 AM
hi

i amm trying to do a javascript that needs to move items between 2 lists
I am catering for a multiple select here. However, even though the script works for a single

select, it doesnt work for multiple. Can someone help?

for single select:


var selectedList;
var availableList;
function createListObjects(){
availableList = document.getElementById("availableOptions");
selectedList = document.getElementById("selectedOptions");
}

function addAttribute(){
var selIndex = selectedList.selectedIndex;
if(selIndex < 0)
return;
availableList.appendChild(selectedList.options.item(selIndex))
selectNone(selectedList,availableList);
setSize(availableList,selectedList);
}

for multiple select


function addAttribute(){

for (int i=0; i<selectedList.length; i++){

var addIndex[i] = availableList.selectedIndex[i];

if(addIndex < 0)
return;

selectedList.appendChild(availableList.options.item(addIndex[i]));
}
selectNone(selectedList,availableList);
setSize(selectedList,availableList);
}

Kravvitz
07-28-2006, 03:14 AM
selectedIndex is the index number of the first selected option.

You need to loop through the options and check their selected property.

newbie2006
07-28-2006, 04:01 AM
function addAttribute(){

for (i=0; i<availableList.length; i++){
if(availableList.options[i].selected.length==0)
return;

if (availableList.options[i].selected)
selectedList.appendChild(availableList.options[i]);
}
selectNone(selectedList,availableList);
setSize(selectedList,availableList);
}


Do you mean something like this? i tried that and it still selects only the first option that is selected.

Appreciate your help.

Kravvitz
07-28-2006, 04:12 AM
The selected property is boolean (true/false). It doesn't have a length property.

Remove this:

if(availableList.options[i].selected.length==0)
return;

newbie2006
07-28-2006, 04:24 AM
thanks for ur prompt reply even after removing this still doesnt work

I notice something weird which is for example. if i select 4 options (a, b,c d), then only 2 (a,c ) will be appended. If i select e.g. 7 then 4 will be added (a,c,e,g)

In fact the problem is such that it works when i select non-consecutive options. e.g. a, d, f but it doesnt work when i select e.g. a, b

Kravvitz
07-28-2006, 05:04 AM
Oh! The problem is that when you remove one the number changes and you increment it as well. Try looping through the options backwards.


for (i=availableList.length-1; i>=0; i--){

newbie2006
07-28-2006, 06:41 AM
thanks so much. it's working now



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum