473,394 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

JavaScript bracket notation

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
3 3237
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: meltedown | last post by:
Theres something very basic about javascript arrays I'm missing. The value of unit.value is 17.00 and value of the qty.value is 5 and I put these values into an array: myarray.value]=qty.value;...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.