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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

removing options from select list box

Status
Not open for further replies.

alfalf

Programmer
Mar 6, 2003
155
BA
Hello.
I have two select option list boxes. Like:
- PC Elements and
- Elements.

When user select an PC element, Elements select list box is filled with those elements belonging to a selected PC Elements group.

My problem: not all Elements array consits of same number of options. Some are extra. I need to remove those extra from same select list.

Now, I have this javascript code for removing options:

function removeOption(arr, element)
{
for(i=0; i<arr.length; i++){
if(arr.search(';'+element)!=-1){
arr1 = arr.slice(0, i);
arr2 = arr.slice(i+1);
arr = arr1.concat(arr2);
return arr;
}
}
}

(as my options are stored in an arrays such as:
listOfOptions_motherboards = new Array(
&quot;ASUS A7VK133;44&quot;,
&quot;ASUS blah blah;45&quot;
);)

I call removeOption when building new list such as:


PCGroup = new Array();
PCGroup = PCGroup.concat(listOfOptions_motherboards);
PCGroup = removeElement(PCGroup, '25'); /*extra*/
PCGroup = removeElement(PCGroup, '39'); /*extra*/
return PCGroup;


But this doesn't work. I get my select list filled OK (when loaded) but I get 'error on page' when function removeObjects is parsed.
Perhaps my code is written false, HELP!

Thx.

 
three errors I see:

1. &quot;removeElement&quot; should be &quot;removeOption&quot;
PCGroup = removeElement(PCGroup, '25'); /*extra*/
PCGroup = removeElement(PCGroup, '39'); /*extra*/

2. move your return statement in removeOption so it returns the array even if element is not found:
function removeOption(arr, element)
{
for(i=0; i<arr.length; i++){
if(arr[ i ].search(';'+element)!=-1){
arr1 = arr.slice(0, i);
arr2 = arr.slice(i+1);
arr = arr1.concat(arr2);
}
}
return arr;
}


3. &quot;return&quot; outside of a function is illegal (unless you just copied this snippet out of a function)
PCGroup = new Array();
PCGroup = PCGroup.concat(listOfOptions_motherboards);
PCGroup = removeElement(PCGroup, '25'); /*extra*/
PCGroup = removeElement(PCGroup, '39'); /*extra*/
return PCGroup;


=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Ok! Thanks. Those were syntax errors. And I have them corrected before but was copying script from two different files and have presented it bad.
So:
1 - it is removeOption here.
2 - that is the problem, later, down explained
3 - this part of code is in function (I call it with onChange event (on first select option).

2:
It seem to me that I did something wrong with removeOption function or with Arrays because I tested it and it seem that a function removeOption cannot find those indexes (but they do appear in my second select list box).

Perhaps I should try to examine more my code, and then if no success will post it in full.

THANKS ANYWAY.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top