So simple it is killing me! > dynamic object reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Hnatt

    So simple it is killing me! > dynamic object reference

    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.myobje ct.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


  • Mike Hnatt

    #2
    Thanks HikksNotAtHome

    Nevermind, I found the answer in another thread here:
    document.getEle mentById("myob" + "ject").innerTe xt = "43";

    Thanks HikksNotAtHome!

    "Mike Hnatt" <dog3@gladsto ne-inc.com> wrote in message
    news:vo9hmsg90f at37@corp.super news.com...[color=blue]
    > 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.myobje ct.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
    >
    >[/color]


    Comment

    • HikksNotAtHome

      #3
      Re: Thanks HikksNotAtHome

      In article <vo9joo417ltl31 @corp.supernews .com>, "Mike Hnatt"
      <dog3@gladsto ne-inc.com> writes:
      [color=blue]
      >Nevermind, I found the answer in another thread here:
      >document.getEl ementById("myob " + "ject").innerTe xt = "43";
      >
      >Thanks HikksNotAtHome![/color]

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

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

      document.getEle mentById(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

      Comment

      • Laurent Bugnion, GalaSoft

        #4
        Re: Thanks HikksNotAtHome

        Hi,

        Mike Hnatt wrote:
        [color=blue][color=green]
        >>I thought it would be something like an "eval" but it is not. I want to
        >>refer to an object called "myobject".[/color][/color]
        [color=blue]
        > Nevermind, I found the answer in another thread here:
        > document.getEle mentById("myob" + "ject").innerTe xt = "43";[/color]

        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

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: Thanks HikksNotAtHome

          "Laurent Bugnion, GalaSoft" <galasoft-LB@bluewin_NO_S PAM.ch> writes:
          [color=blue]
          > Note however that not all browsers allow getElementById. An
          > alternative might be[/color]
          [color=blue]
          > window[ "myob" + "ject" ][/color]

          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.).
          [color=blue]
          > All global objects are actually stored in the global object. In the
          > case of the browser, the global object is the Window.[/color]

          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.getEle mentById, document.all
          (IE4), and document.layers (Netscape 4).

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

          Comment

          • cwdjr

            #6
            Re: Thanks HikksNotAtHome

            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.

            Comment

            • Lasse Reichstein Nielsen

              #7
              Re: Thanks HikksNotAtHome

              cwdjrxyz@yahoo. com (cwdjr) writes:
              [color=blue]
              > 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.[/color]

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

              For maximal compatability, I would use a function like

              function getElement(id) {
              if (document.getEl ementById) {return document.getEle mentById(id);}
              if (document.all) {return document.all[id];}
              if (document.layer s) {return document.layers[id];}
              return window[id];
              }
              [color=blue]
              > The CSS support is very strange.[/color]
              ....
              Sounds like an early, and slightly mangled, version of IE 4 :)

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

              Comment

              • Mike Hnatt

                #8
                Re: Thanks HikksNotAtHome

                Thanks all. I used Lasse's maximal compatibility and it works great!
                Thanks again,
                Mike


                "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
                news:zngae67s.f sf@hotpop.com.. .[color=blue]
                > cwdjrxyz@yahoo. com (cwdjr) writes:
                >[color=green]
                > > 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.[/color]
                >
                > Thank you for that information. I though IE 4 was the only browser to
                > support document.all and not document.getEle mentById.
                >
                > For maximal compatability, I would use a function like
                >
                > function getElement(id) {
                > if (document.getEl ementById) {return document.getEle mentById(id);}
                > if (document.all) {return document.all[id];}
                > if (document.layer s) {return document.layers[id];}
                > return window[id];
                > }
                >[color=green]
                > > The CSS support is very strange.[/color]
                > ...
                > Sounds like an early, and slightly mangled, version of IE 4 :)
                >
                > /L
                > --
                > Lasse Reichstein Nielsen - lrn@hotpop.com
                > Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
                > 'Faith without judgement merely degrades the spirit divine.'[/color]


                Comment

                Working...