473,796 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FAQ Topic - How do I access a property of an object using a string?

-----------------------------------------------------------------------
FAQ Topic - How do I access a property of an object using a string?
-----------------------------------------------------------------------

There are two equivalent ways to access properties: the dot
notation and the square bracket notation. What you are looking
for is the square bracket notation in which the dot, and the
identifier to its right, are replaced with a set of square
brackets containing a string. The value of the string matches
the identifier. For example:-

//dot notation
var bodyElement = document.body;

//square bracket notation, using an expression
var bodyElement = document["bo"+"dy"];

http://www.jibbering.com/faq/faq_not..._brackets.html
===
Postings such as this are automatically sent once a day. Their
goal is to answer repeated questions, and to offer the content to
the community for continuous evaluation/improvement. The complete
comp.lang.javas cript FAQ is at http://www.jibbering.com/faq/.
The FAQ workers are a group of volunteers.

Nov 17 '06
19 2637
In article <Y9************ **@invalid.uk.c o.demon.merlyn. invalid>, Dr J R
Stockton <jr*@merlyn.dem on.co.ukwrites
>In message <45************ ***********@new s.sunsite.dk>, Sat, 18 Nov 2006
00:00:01, FAQ server <ja********@dot internet.bewrit es
>>
There are two equivalent ways to access properties: the dot notation
and the square bracket notation. What you are looking for is the square
bracket notation in which the dot, and the identifier to its right, are
replaced with a set of square brackets containing a string. The value
of the string matches the identifier. For example:-

//dot notation
var bodyElement = document.body;

//square bracket notation, using an expression
var bodyElement = document["bo"+"dy"];

http://www.jibbering.com/faq/faq_not..._brackets.html

Properties can be addressed by dot notation and by square bracket
notation.

Dot notation is appropriate where the "term" is known at programming
time.
Dot notation is not appropriate when the property name does not obey the
rules for identifiers.

>Square bracket notation is necessary if the "term" must be supplied at
run time.
"term" is pretty woolly. What's wrong with property name? It's explicit
and accurate.

John
--
John Harris
Nov 19 '06 #11
In article <GO************ ********@telcov e.net>, Randy Webb
<Hi************ @aol.comwrites

<snip>
>PHP's array-ness of inputs comes to mind first. If the name of the
inputs is "myInput[]" then you have to use bracket notation to access
it using it's name but the "term" will be known at programming time.
<snip>

What is it about PHP that forces it to dictate javascript property
names?

John
--
John Harris
Nov 19 '06 #12
John G Harris wrote:
What is it about PHP that forces it to dictate javascript property
names?
Programmer convenience - if you name an array of checkboxes "input[]" for
example, then they will be automagically converted to an array on the server
side. And the input name is a property or the form.elements[] collection
which is accessed via javascript.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Nov 19 '06 #13
John G Harris said the following on 11/19/2006 3:13 PM:
In article <GO************ ********@telcov e.net>, Randy Webb
<Hi************ @aol.comwrites

<snip>
>PHP's array-ness of inputs comes to mind first. If the name of the
inputs is "myInput[]" then you have to use bracket notation to access
it using it's name but the "term" will be known at programming time.
<snip>

What is it about PHP that forces it to dictate javascript property
names?
When form elements are submitted to PHP on the server, any like names
followed by [] are automatically put into a PHP array. As a result, you
see NAME attributes that look like this: 'myInput[]'. It has nothing to
do with dictating javascript property names, it has to do with having to
use bracket notation to access that form element when it is named like that.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 19 '06 #14
Dr J R Stockton said the following on 11/19/2006 1:22 PM:
In message <GO************ ********@telcov e.net>, Sat, 18 Nov 2006
19:46:57, Randy Webb <Hi************ @aol.comwrites
>Dr J R Stockton said the following on 11/18/2006 3:44 PM:

<snip>
>>Square bracket notation is necessary if the "term" must be supplied at
run time.

That is not always true. Square bracket notation is necessary if the
"term" has characters in that JS doesn't like. PHP's array-ness of
inputs comes to mind first. If the name of the inputs is "myInput[]"
then you have to use bracket notation to access it using it's name but
the "term" will be known at programming time.

That very specific issue is dealt with in Section 4.25.

When I wrote 'Square bracket notation is necessary if the "term" must be
supplied at run time', I meant 'Square bracket notation is necessary if
the "term" must be supplied at run time'.
And my point is that your statement is false. It implies that is the
only time it is necessary and that isn't true. Perhaps if I had quoted
the previous line to the one I quoted you wouldn't be splitting hairs
about what I wrote. Because the previous line is also patently false.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 19 '06 #15
In comp.lang.javas cript message
<sg************ **@jgharris.dem on.co.uk.nospam .invalid>, Sun, 19 Nov 2006
19:58:59, John G Harris <jo**@nospam.de mon.co.ukwrote:
>In article <Y9************ **@invalid.uk.c o.demon.merlyn. invalid>, Dr J R
Stockton <jr*@merlyn.dem on.co.ukwrites
>>Properties can be addressed by dot notation and by square bracket
notation.

Dot notation is appropriate where the "term" is known at programming
time.

Dot notation is not appropriate when the property name does not obey the
rules for identifiers.
Then :
"Dot notation is appropriate where the "term" is an identifier known at
programming time."

>>Square bracket notation is necessary if the "term" must be supplied at
run time.

"term" is pretty woolly. What's wrong with property name? It's explicit
and accurate.
I put it in quotes because something better failed to occur to me at the
time! Though, in the context, "property" might be considered implicit.

To allow for the Merkins, the FAQ should contain, in Section 1, the
observation that "if" does not mean "if and only if"; and the "if"s in
the more rigorous parts of the FAQ should be scrutinised for compliance.

In 4.7, for example, "if" is correct; the condition is sufficient but
not necessary (example : for integer N, (N - N/2 - N/2) is exactly zero,
though if N is 1 then N/2 is non-integer).

But in 4.31 the second "if" should be "whether".

<FAQENTRY>
4.4, 4.10 Subject : if -whether

S.1 para 4 : false.

S.1 para 7 : We use those conventions. "Suggestion s" is undefined. If
the browser supports CSS, then by default one will see ...

In 4.31 the second "if" should be "whether".

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demo n.co.uk/- FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
Nov 20 '06 #16
Dr J R Stockton said the following on 11/20/2006 11:50 AM:

<snip>
To allow for the Merkins, the FAQ should contain, in Section 1, the
observation that "if" does not mean "if and only if"; and the "if"s in
the more rigorous parts of the FAQ should be scrutinised for compliance.
And to allow for the anally retentive people among us Section 1 should
contain a note about being anally retentive!

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 20 '06 #17
In article <ej*********@ne ws3.newsguy.com >, Matt Kruse
<ne********@mat tkruse.comwrite s
>John G Harris wrote:
>What is it about PHP that forces it to dictate javascript property
names?

Programmer convenience - if you name an array of checkboxes "input[]" for
example, then they will be automagically converted to an array on the server
side. And the input name is a property or the form.elements[] collection
which is accessed via javascript.
Thank you.

This looks suspiciously like eval to me :-)

John

PS See! It isn't only VK who gets thanked.
--
John Harris
Nov 20 '06 #18
In comp.lang.javas cript message <Oo************ ********@telcov e.net>,
Sun, 19 Nov 2006 18:43:54, Randy Webb <Hi************ @aol.comwrote:
>Dr J R Stockton said the following on 11/19/2006 1:22 PM:
>In message <GO************ ********@telcov e.net>, Sat, 18 Nov 2006
19:46:57, Randy Webb <Hi************ @aol.comwrites
>>Dr J R Stockton said the following on 11/18/2006 3:44 PM:

<snip>

Square bracket notation is necessary if the "term" must be supplied at
run time.

That is not always true. Square bracket notation is necessary if the
"term" has characters in that JS doesn't like. PHP's array-ness of
inputs comes to mind first. If the name of the inputs is "myInput[]"
then you have to use bracket notation to access it using it's name
but the "term" will be known at programming time.

That very specific issue is dealt with in Section 4.25.
When I wrote 'Square bracket notation is necessary if the "term"
must be supplied at run time', I meant 'Square bracket notation is
necessary if the "term" must be supplied at run time'.

And my point is that your statement is false. It implies that is the
only time it is necessary and that isn't true. Perhaps if I had quoted
the previous line to the one I quoted you wouldn't be splitting hairs
about what I wrote. Because the previous line is also patently false.

No : the problem is that not only can you not write English, but also
you cannot even read it.

The word "if" does not mean "if and only if".

My statement quoted at the top is equivalent to

If the "term" must be supplied at run time, then square bracket
notation is necessary.

But, of course, "then" does not mean "then and only then".

Consider "If a person lives in Utah, then he/she is resident in
America.". That's generally believed to be true, even I presume in
Iowa, where dwellers are also American residents.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demo n.co.uk/- FAQish topics, acronyms, & links.
For news:borland.*, use their server newsgroups.borl and.com ; but first read
Guidelines <URL:http://www.borland.com/newsgroups/guide.htmlff. with care.
Nov 20 '06 #19
Dr J R Stockton said the following on 11/20/2006 3:50 PM:
In comp.lang.javas cript message <Oo************ ********@telcov e.net>,
Sun, 19 Nov 2006 18:43:54, Randy Webb <Hi************ @aol.comwrote:
>Dr J R Stockton said the following on 11/19/2006 1:22 PM:
>>In message <GO************ ********@telcov e.net>, Sat, 18 Nov 2006
19:46:57, Randy Webb <Hi************ @aol.comwrites
Dr J R Stockton said the following on 11/18/2006 3:44 PM:

<snip>

Square bracket notation is necessary if the "term" must be supplied at
run time.
That is not always true. Square bracket notation is necessary if the
"term" has characters in that JS doesn't like. PHP's array-ness of
inputs comes to mind first. If the name of the inputs is "myInput[]"
then you have to use bracket notation to access it using it's name
but the "term" will be known at programming time.

That very specific issue is dealt with in Section 4.25.
When I wrote 'Square bracket notation is necessary if the "term"
must be supplied at run time', I meant 'Square bracket notation is
necessary if the "term" must be supplied at run time'.
And my point is that your statement is false. It implies that is the
only time it is necessary and that isn't true. Perhaps if I had quoted
the previous line to the one I quoted you wouldn't be splitting hairs
about what I wrote. Because the previous line is also patently false.
I snipped everything you said that made any sense. You are a half wit
sometimes.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 21 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4759
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much appreciated. Thanks in advance
7
8873
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I want my users to be able to select a report, click on a command button on a form, which will then automatically create the report as a pdf file and save it to the user's machine. I am using Adobe Acrobat (5.0 I think) and have Adobe Distiller as a
11
6603
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am not logged into the server, Access is not able to print to the printer. The error is pretty...
8
3995
by: doomx | last post by:
I'm using SQL scripts to create and alter tables in my DB I want to know if it's possible to fill the description(like in the Create table UI) using these scripts. EX: CREATE TABLE( Pk_myPrimaryKey INTEGER CONSTRAINT pk PRIMARY KEY DESCRIPTION 'This is the primary key of the table',
8
376
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I detect Opera/Netscape/IE? ----------------------------------------------------------------------- The « navigator » object contains strings which specify the browser and version; however, this is in general not very genuine. Mozilla (and therefore Netscape 6+) allows this to be freely set, and Opera and IE allow it to be modified. There are...
7
402
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I access a property of an object using a string? ----------------------------------------------------------------------- There are two ways to access properties: the dot notation and the square bracket notation. What you are looking for is the square bracket notation in which the dot, and the identifier to its right, are replaced with a set of...
0
9679
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9527
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10453
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10172
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.