PDA

View Full Version : Can't delete select option using its id


SYP}{ER
02-09-2003, 02:56 AM
[URL removed. The page has changed and has been made secure]

I edit their text, value, and id. I just can't delete the whole object from the select.

Theoretically, document.getElementById('optionID') = null should work. However, I get this error:

Wrong number of arguments or invalid property assignment on line 72 (That's the document.getElementById(cats[what][i]) = null line.)

Any help would be greatly appreciated. I'm off to bed for now though :)

Thanks.

jkd
02-09-2003, 03:34 AM
You just can't set references to null. Use DOM scripting throughout the entire thing:

var opt = document.getElementById('optionID');
opt.parentNode.removeChild(opt);

SYP}{ER
02-09-2003, 12:07 PM
Ah, thank you :)

I think this summer I might just site myself down and read up on all this JS stuff :P

I learned HTML, XHTML (as if XHTML is hard to learn...) and PHP as I went along. It was easy, as the documentation was very good.

JS seems to make it very tough to just learn as you go :S Slow, for sure.

SYP}{ER
02-09-2003, 03:11 PM
Ok, same script - new problem.

function in_array (string,arrToSearch) {
retValue = 0;
for (i=0;i < arrToSearch.length;i++) {
if (arrToSearch[i] == string){
retValue = 1;
break;
}
}
return retValue;
}

That checks to see if an element exists in an array. Returns 0 on false, 1 on true.

Now, I use it when making my options like this:

l = obj.options.length;
for (i=0;i<cats[what].length;i++){
obj.options[l] = new Option (cats[what][i],cats[what][i]);
if (in_array (cats[what][i], beenSelected)==1) obj.options[l].selected = true;
obj.options[l].id = cats[what][i];
if (typeof optionsArr[what]=='undefined') optionsArr[what] = new Array ();
l++;
}

beenSelected is created successfully using PHP. It writes a JS array to the script with the list of selected options which had been selected when the form was submitted.

The problem
Somehow, calling "in_array (cats[what][i], beenSelected)" (even if just in an alert) creates an infinite loop on the page and it spits out infinite options which are selected... in_array is returning 0 though...