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

So simple it is killing me! > dynamic object reference

P: n/a
I thought it would be something like an "eval" but it is not. I want to
refer to an object called "myobject".

I can normally do this:

document.myobject.innerText= "43"

But since I don't know the name of the object, I need something like:

document["myob" + "ject"].innerText= "43"

But it doesn't work. Can anyone help? Thanks,
Mike
Jul 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Nevermind, I found the answer in another thread here:
document.getElementById("myob" + "ject").innerText = "43";

Thanks HikksNotAtHome!

"Mike Hnatt" <do**@gladstone-inc.com> wrote in message
news:vo************@corp.supernews.com...
I thought it would be something like an "eval" but it is not. I want to
refer to an object called "myobject".

I can normally do this:

document.myobject.innerText= "43"

But since I don't know the name of the object, I need something like:

document["myob" + "ject"].innerText= "43"

But it doesn't work. Can anyone help? Thanks,
Mike

Jul 20 '05 #2

P: n/a
In article <vo************@corp.supernews.com>, "Mike Hnatt"
<do**@gladstone-inc.com> writes:
Nevermind, I found the answer in another thread here:
document.getElementById("myob" + "ject").innerText = "43";

Thanks HikksNotAtHome!


Ummm, welcome, whatever I did.
BTW, what did I do?

Not sure why you are breaking the string like that though.

document.getElementById(var1 + var2).innerText = "43";

will give you a reference to what ever var1+var2 evaluates to. Also, innerText
is IE only.

Or is that what you meant?
--
Randy
Jul 20 '05 #3

P: n/a
Hi,

Mike Hnatt wrote:
I thought it would be something like an "eval" but it is not. I want to
refer to an object called "myobject".
Nevermind, I found the answer in another thread here:
document.getElementById("myob" + "ject").innerText = "43";


Note however that not all browsers allow getElementById. An alternative
might be

window[ "myob" + "ject" ]

All global objects are actually stored in the global object. In the case
of the browser, the global object is the Window.

HTH,

Laurent
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #4

P: n/a
"Laurent Bugnion, GalaSoft" <galasoft-LB@bluewin_NO_SPAM.ch> writes:
Note however that not all browsers allow getElementById. An
alternative might be window[ "myob" + "ject" ]
Far fewer browsers allow for that, and of those only one allow
innerText. Generally, only IE allow for the innerText property (and
Opera 7).

All modern browsers understand getElementById (Mozilla, Opera 6+, IE
5+, etc.).
All global objects are actually stored in the global object. In the
case of the browser, the global object is the Window.


Correct. But named elements are not usually made available as global
variables. Accessing elements as global variables should only be the
last resort, after having tried document.getElementById, document.all
(IE4), and document.layers (Netscape 4).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

P: n/a
Lasse Reichstein Nielsen states: "All modern browsers understand
getElementById (Mozilla, Opera 6+, IE 5+, etc.).".I have no argument
with this. However in the US and Canada about one million use the
MSNTV(former WebTV) service. Even the latest 2.8.1 version of this
browser is very outdated in support of both JS and CSS. It does not
support getElementById at all - only document.all. The CSS support is
very strange. It does not support many things in CSS1, such as fancy
borders. However it supports positioning of CSS2 fairly well. Also it
will support some CSS style at the local division level but not at the
global level in the head. The MSNTV service tends to be used by first
time users who tend to either be young people or older adults who have
never used a computer before. The MSNTV developer site has an MSNTV
viewer that can be downloaded to a computer if one wants a fair idea
of how their page might appear on MSNTV. Set top boxes have not grown
much in usage in recent years. Unless MSNTV comes out with both a new
browser and an improved box soon, I think it may slowly fade away.
Jul 20 '05 #6

P: n/a
cw******@yahoo.com (cwdjr) writes:
However in the US and Canada about one million use the MSNTV(former
WebTV) service. Even the latest 2.8.1 version of this browser is
very outdated in support of both JS and CSS. It does not support
getElementById at all - only document.all.
Thank you for that information. I though IE 4 was the only browser to
support document.all and not document.getElementById.

For maximal compatability, I would use a function like

function getElement(id) {
if (document.getElementById) {return document.getElementById(id);}
if (document.all) {return document.all[id];}
if (document.layers) {return document.layers[id];}
return window[id];
}
The CSS support is very strange.

....
Sounds like an early, and slightly mangled, version of IE 4 :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #7

P: n/a
Thanks all. I used Lasse's maximal compatibility and it works great!
Thanks again,
Mike
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:zn**********@hotpop.com...
cw******@yahoo.com (cwdjr) writes:
However in the US and Canada about one million use the MSNTV(former
WebTV) service. Even the latest 2.8.1 version of this browser is
very outdated in support of both JS and CSS. It does not support
getElementById at all - only document.all.


Thank you for that information. I though IE 4 was the only browser to
support document.all and not document.getElementById.

For maximal compatability, I would use a function like

function getElement(id) {
if (document.getElementById) {return document.getElementById(id);}
if (document.all) {return document.all[id];}
if (document.layers) {return document.layers[id];}
return window[id];
}
The CSS support is very strange.

...
Sounds like an early, and slightly mangled, version of IE 4 :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.