JavaScript bracket notation

L

larry

Hi,

I have difficulty understanding how square brackets work in JavaScript
and this is causing me difficulty retrieving the values of the options
of my Select controls.

My form contains many select boxes. Their names are stored in
variables(in this case concat is the select control name)
and I have no trouble accessing methods for the select object ie:
document.Allan.elements[concat].selectedIndex;

I would like to get the value of the selectedIndex but I am having
difficulty because I don't know the right syntax. I'm looking for a
statement like the one that follows but uses variables

document.form.theDay.options[2].text


Could some one help me use the right syntax?

Thanks,
Larry
 
R

Randy Webb

larry said the following on 8/8/2005 3:22 PM:
Hi,

I have difficulty understanding how square brackets work in JavaScript
and this is causing me difficulty retrieving the values of the options
of my Select controls.

My form contains many select boxes. Their names are stored in
variables(in this case concat is the select control name)
and I have no trouble accessing methods for the select object ie:
document.Allan.elements[concat].selectedIndex;

I would like to get the value of the selectedIndex but I am having
difficulty because I don't know the right syntax. I'm looking for a
statement like the one that follows but uses variables

document.form.theDay.options[2].text


Could some one help me use the right syntax?

document.forms[var1Here].elements[var2Here].***

Where *** is any of the properties of the elements objects.
 
R

RobG

larry said:
Hi,

I have difficulty understanding how square brackets work in JavaScript
and this is causing me difficulty retrieving the values of the options
of my Select controls.

My form contains many select boxes. Their names are stored in
variables(in this case concat is the select control name)
and I have no trouble accessing methods for the select object ie:
document.Allan.elements[concat].selectedIndex;

That accesses the selectedIndex property, not a method. See below.
I would like to get the value of the selectedIndex but I am having
difficulty because I don't know the right syntax. I'm looking for a
statement like the one that follows but uses variables

document.form.theDay.options[2].text


Could some one help me use the right syntax?

Use square brackets to access properties, round brackets to use
methods and quote string literals:

<URL:http://www.jibbering.com/faq/faq_notes/square_brackets.html>

Using IE it is very easy to get confused as often the two appear to be
treated the same.
 
M

Matt Kruse

larry said:
document.Allan.elements[concat].selectedIndex;
I would like to get the value of the selectedIndex but I am having
difficulty because I don't know the right syntax. I'm looking for a
statement like the one that follows but uses variables
document.form.theDay.options[2].text

The simple answer to your question is:

var i = document.forms['Allan'].elements[concat].selectedIndex;
alert(document.forms['Allan'].elements[concat].options.value);

or,

var el = document.forms['Allan'].elements[concat];
alert(el.options[el.selectedIndex].value);

Assumptions:
1) It is a single-select list, not multi-select
2) There is an option selected
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top