/    Sign up×
Community /Pin to ProfileBookmark

Variable and Object Names – Help

I have the following issue:

I would like to access an object, but the object name is stored in a variable. How can I do this?

Example:

var mystr = “form1”;
document.{mystr}.object.attribute

I would like to access document.form1.object.attribute

The object itself is NOT a form. it’s a generic object.

I’ve used the collections before, but only with a position variable…

document.collection[2].object.attribute

but don’t know how to access by name. Is this possible? do I have to create an array of all my objects and positions so I reference them by position?

Thanks for all your help.

A.-

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51Apr 04.2006 — baisc idea:

document.forms[variableName].elements[elementName].value

Eric
Copy linkTweet thisAlerts:
@aeavaauthorApr 04.2006 — What you've said works under the browser environment. I am, however, working with Laszlo, so the structure is a bit different.

I was hoping for a more generic approach to it.

I have a collection named subnodes in my environment, equivalent to elements, I guess. I have this:

<view layout="axis:x" x="5" id="playfield">
<mybutton btext="*" />
<mybutton name="BAD" btext="BAD" />
<mybutton name="HHRD" btext="HHRD" />
<mybutton name="MAD" btext="MAD" />
<mybutton name="MBISD" btext="MBISD" />
<mybutton name="MSD" btext="MSD" />
<mybutton name="NSD" btext="NSD" />
<mybutton name="OD" btext="OD" />
<mybutton name="TAD" btext="TAD" />
</view>


I want to access, via javascript a particular object.

I can do "playfield.BAD.btext" and I would get the value for that attribute

I could also do "playfield.subnodes[2].btext" and I would get the same.

subnodes is not taking names, unfortunately, just position [#].
×

Success!

Help @aeava spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...