PDA

View Full Version : Options object of select menu -> "no properties" ?



Space Goat
02-01-2008, 07:40 PM
I'm writing a form that uses JavaScript to pre-fill elements with cookie values. I have it mostly working, but I can't get the code to pre-choose select menu entries to work.

Here's my JS code:


function cookieMake () {

var formelms = new Array();

formelms[0] = 'input';
formelms[1] = 'textarea';
formelms[2] = 'select';

for (b=0; b < formelms.length; b++) {

var tag = formelms[b];
var elms = document.getElementsByTagName(tag);
var count = elms.length;

for (var i=1; i<count; i++) {

var item = elms.item(i);
var title = item.name;
var type = item.type;
var value = item.value;

if (tag == 'input') {

if (type == 'radio' || type == 'text') {

ints.setSubValue (title, value);

}

} else {

ints.setSubValue (title, value);

}

}

}

}

function cookieFill () {

if (ints.cookieExists ('ints')) {

var formelms = new Array();

formelms[0] = 'input';
formelms[1] = 'textarea';
formelms[2] = 'select';

for (b=0; b < formelms.length; b++) {

var tag = formelms[b];
var elms = document.getElementsByTagName(tag);
var count = elms.length;

for (var i=1; i<count; i++) {

var item = elms.item(i);
var title = item.name;
var type = item.type;

if (cookval = ints.getSubValue (title)) {

switch (type) {

case 'select-one':
var selectarr = document.forms['theform'].title;
var optscount = selectarr.options.length;

for (var a=1; a<optscount; a++) {

if (selectarr.options[a].value == cookval) {

selectarr.selectedIndex = a;

}

}
break;

case 'text':
item.value = cookval;
break;

case 'textarea':
item.value = cookval;
break;

}

}

}

}

}

}

Firefox's Firebug shoots me the error, "selectarr.options has no properties," for the line under case "select-one" in cookieFill:


var optscount = selectarr.options.length;

If I use the full name of the element instead of a variable, the same kind of error occurs.

If I try to get the length for the select element itself, instead of the options object, the error doesn't appear; the result is just 0.

Can anyone help me out here?

BYK
02-01-2008, 08:16 PM
Have you tried using elms[i] instead of elms.item(i) ?

Also avoid using generic variable names such as value, type, name etc. Some of them might be reserved and that will cause a failiure in your script.