By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,287 Members | 1,514 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,287 IT Pros & Developers. It's quick & easy.

JavaScript bracket notation

P: n/a
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

Aug 8 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Aug 8 '05 #2

P: n/a
larry wrote:
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.
--
Rob
Aug 9 '05 #3

P: n/a
larry wrote:
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[i].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

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Aug 9 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.