PDA

View Full Version : Testing a multiple Select


PMowbray
03-04-2003, 11:16 PM
HI:

I am trying to test (validate) the values in a select:

eg: document.selectname.value

The problem is that the select name includes [] which java script interprets differently.

ie document.ReportTypes[].value

Is there some way around this?

Thanks,
Perry

chrismiceli
03-05-2003, 12:38 AM
don't name it with the [] in its name, you might also want to try to escape the characters, like this
document.whatever\[\].value;

cheesebagpipe
03-05-2003, 12:57 AM
Name it whatever you like; just use the appropriate form of referencing:

document.form_name['ReportTypes[]'].value

Associative array references ("hash") allow virtually any string characters, since the "key" - the string index - is, erm, a string. This is how the system (JS) references object properties. Dot syntax is restricted to tokens - variable names, i.e., alphanumerics, dollar signs, underscores and - surprise! - dots. You'll get better browser support with this approach:

var oSel = document.form_name['ReportTypes[]'];
var selVal = oSel[oSel.selectedIndex].value;

PMowbray
03-05-2003, 01:15 AM
Hi!

Thanks for the reply.

Originally posted by cheesebagpipe
Name it whatever you like; just use the appropriate form of referencing:

document.form_name['ReportTypes[]'].value

Associative array references ("hash") allow virtually any string characters, since the "key" - the string index - is, erm, a string. This is how the system (JS) references object properties. Dot syntax is restricted to tokens - variable names, i.e., alphanumerics, dollar signs, underscores and - surprise! - dots. You'll get better browser support with this approach:

var oSel = document.form_name['ReportTypes[]'];
var selVal = oSel[oSel.selectedIndex].value;

I also discovered that I can do:
document.form_name.elements[5].value

I just had to hard code the '5', but it works.

There's always more than one way to skin a cat! :thumbsup:

Thanks for your help

Perry

cheesebagpipe
03-05-2003, 01:24 AM
That's where the actual element object (of type Select) is stored. The whole purpose of referencing by name is to avoid using the .elements array (with integer indices). Add a new element to the first part of your form and you'll find out why...;)

PMowbray
03-05-2003, 02:05 AM
Hi:

You're right, I was just struggling to find a way to do it. :o

At least it works, but I'll go back and clean it up!

Thanks again,
Perry


Originally posted by cheesebagpipe
That's where the actual element object (of type Select) is stored. The whole purpose of referencing by name is to avoid using the .elements array (with integer indices). Add a new element to the first part of your form and you'll find out why...;)