Addressing an Array from Function Arguments?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Altschwager

    Addressing an Array from Function Arguments?

    Hi,

    I desperately need some help with what I assume to be a minor problem
    that I just can't solve.

    I'm writing a page where on mouseover a link, an image needs to be
    displayed and some text, which is defined inside an array, sould
    appear inside a table cell (whose id is "tdDesc").

    That's basically what's in the HTML-file:

    <body onload="resetDe sc('WS_Standard ','3');">
    <a href="#" onmouseover="sh owDesc('WS_Stan dard','1');"
    onmouseover="sh owDesc('WS_Stan dard','1');">
    Link no. 1</a>

    Here's what's in the js-File.

    01 function showDesc(typeDe sc,noDesc) {
    02 var imgDesc = ('img' + typeDesc + '_' + noDesc);
    03 document.getEle mentById('tdDes c').innerHTML =
    WS_Standard[noDesc];
    04 document.getEle mentById(imgDes c).style.displa y = "";
    05 }
    06 function hideDesc(typeDe sc,noDesc) {
    07 var imgDesc = ('img' + typeDesc + '_' + noDesc);
    08 document.getEle mentById('tdDes c').innerHTML = WS_Standard[0];
    09 document.getEle mentById(imgDes c).style.displa y = "none";
    10 }
    11 function resetDesc(typeD esc,totalDesc) {
    12 for (var i = 1; i <= totalDesc; i++) {
    13 var imgDesc = ('img' + typeDesc + '_' + i);
    14 document.getEle mentById(imgDes c).style.displa y = "none";
    15 }
    16 document.getEle mentById('tdDes c').innerHTML = WS_Standard[0];
    17 }
    18 var WS_Standard = new Array();
    19 WS_Standard[0] = "Standard text when no mouse over";
    20 WS_Standard[1] = "Sample Text 1";
    21 WS_Standard[2] = "Sample Text 2";
    22 WS_Standard[3] = "Sample Text 3";

    My problem appears when it comes to adressing the text that is in the
    array, lines 03, 08, 16. I want to use my "typeDesc"-argument to
    adress the array, but whatever I try, it's either an error or I get a
    string "WS_Standar d" or "WS_Standar d[1]" or something, but never the
    text in the array WS_Standard[1].

    What am I doing wrong?

    Any help is highly appreciated!

    Martin
  • Stephen Chalmers

    #2
    Re: Addressing an Array from Function Arguments?

    Martin Altschwager <cosas@altschwa ger.de> wrote in message
    news:bbebb69b.0 502071410.25ca1 280@posting.goo gle.com...
    [color=blue]
    > onmouseover="sh owDesc('WS_Stan dard','1');"[/color]
    [color=blue]
    > 01 function showDesc(typeDe sc,noDesc)[/color]
    {
    [color=blue]
    >03 document.getEle mentById('tdDes c').innerHTML = WS_Standard[noDesc];[/color]

    You're subscripting the array with the string: '1';

    --
    S.C.



    Comment

    • Robert

      #3
      Re: Addressing an Array from Function Arguments?

      In article <42080877$1_3@m k-nntp-2.news.uk.tisca li.com>,
      "Stephen Chalmers" <ignoring@lycos .co.uk> wrote:
      [color=blue]
      > Martin Altschwager <cosas@altschwa ger.de> wrote in message
      > news:bbebb69b.0 502071410.25ca1 280@posting.goo gle.com...
      >[color=green]
      > > onmouseover="sh owDesc('WS_Stan dard','1');"[/color]
      >[color=green]
      > > 01 function showDesc(typeDe sc,noDesc)[/color]
      > {
      >[color=green]
      > >03 document.getEle mentById('tdDes c').innerHTML = WS_Standard[noDesc];[/color]
      >
      > You're subscripting the array with the string: '1';[/color]

      While it isn't standard, that should still work since numeric indexes
      are converted a string value before the actual lookup.

      Robert

      Comment

      • Robert

        #4
        Re: Addressing an Array from Function Arguments?

        In article <bbebb69b.05020 71410.25ca1280@ posting.google. com>,
        cosas@altschwag er.de (Martin Altschwager) wrote:
        [color=blue]
        > Any help is highly appreciated![/color]

        I do not believe that I found any problems with the javascript functions
        you showed. I filled in the missing html and changed one event handler.

        Here is my revised version of the file.

        Robert

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
        <title>Saving stats</title>
        <base href="http://spaceplace.jpl. nasa.gov/en/_images/">
        <script type="text/javascript">
        /*
        My problem appears when it comes to adressing the text that is in the
        array, lines 03, 08, 16. I want to use my "typeDesc"-argument to
        adress the array, but whatever I try, it's either an error or I get a
        string "WS_Standar d" or "WS_Standar d[1]" or something, but never the
        text in the array WS_Standard[1].

        changes
        call onmouseout
        call hideDesc
        you do not show the text to change nor the images. Could have the
        wrong values in the id attributes
        this is a bad variable name tdDesc 'cause it is hard to distingish
        the d from the D.



        */
        var WS_Standard = new Array();
        WS_Standard[0] = "Standard text when no mouse over";
        WS_Standard[1] = "Sample Text 1";
        WS_Standard[2] = "Sample Text 2";
        WS_Standard[3] = "Sample Text 3";


        function showDesc(typeDe sc,noDesc) {
        var imgDesc = ('img' + typeDesc + '_' + noDesc);
        document.getEle mentById('table Description').i nnerHTML =
        WS_Standard[noDesc];
        document.getEle mentById(imgDes c).style.displa y = "";
        }


        function hideDesc(typeDe sc,noDesc) {
        var imgDesc = ('img' + typeDesc + '_' + noDesc);
        document.getEle mentById('table Description').i nnerHTML =
        WS_Standard[0];
        document.getEle mentById(imgDes c).style.displa y = "none";
        }

        function resetDesc(typeD esc,totalDesc) {
        for (var i = 1; i < totalDesc; i++) {
        var imgDesc = ('img' + typeDesc + '_' + i);
        document.getEle mentById(imgDes c).style.displa y = "none";
        }
        document.getEle mentById('table Description').i nnerHTML =
        WS_Standard[0];
        }


        </script>
        </head>
        <body onload="resetDe sc('WS_Standard ',WS_Standard.l ength);">
        <a href="#" onmouseover="sh owDesc('WS_Stan dard',1);"
        onmouseout="hid eDesc('WS_Stand ard',1);">
        Link no. 1</a>

        <p>
        <img
        id="imgWS_Stand ard_1"
        src="common/nasa_header/logo_nasa.gif">
        <br>
        <img
        id="imgWS_Stand ard_2"
        src="common/nasa_header/logo_nasa.gif">
        <br>
        <img
        id="imgWS_Stand ard_3"
        src="common/nasa_header/logo_nasa.gif">
        </p>
        <p>
        <span id="tableDescri ption" class="hidden"> Message to display</span>
        </p>
        </body>
        </html>

        Comment

        • Martin Altschwager

          #5
          Re: Addressing an Array from Function Arguments?

          "Stephen Chalmers" <ignoring@lycos .co.uk> wrote in message news:<42080877$ 1_3@mk-nntp-2.news.uk.tisca li.com>...[color=blue]
          > Martin Altschwager <cosas@altschwa ger.de> wrote in message
          > news:bbebb69b.0 502071410.25ca1 280@posting.goo gle.com...
          >[color=green]
          > > onmouseover="sh owDesc('WS_Stan dard','1');"[/color]
          >[color=green]
          > > 01 function showDesc(typeDe sc,noDesc)[/color]
          > {
          >[color=green]
          > >03 document.getEle mentById('tdDes c').innerHTML = WS_Standard[noDesc];[/color]
          >
          > You're subscripting the array with the string: '1';[/color]

          Hi Stephen,

          Thank you for answering, but I'm sorry, I don't get it. The way the
          function is shown above, it does work. But what needs to be changed in
          my function so that it returns the text in the array that I specify in
          the argument? Something like

          01 function showDesc(typeDe sc,noDesc) {
          03 document.getEle mentById('tdDes c').innerHTML = typeDesc[noDesc];

          These two lines obviously don't work. What's wrong?

          Thank you already for further help!

          Martin

          Comment

          • Martin

            #6
            Re: Addressing an Array from Function Arguments?

            Hi Robert,

            thank you for your reply. As a matter of fact, the .js-file I posted is
            indeed working. My problem lies in lines 3, 8, and 16. Let me
            demonstrate with line 3:

            document.getEle mentById('tdDes c').innerHTML=W S_Standard[noDesc];

            I'm explicitly pointing to the Array WS_Standard, and that works
            perfectly.

            However, I want to use the function later again with another Array, for
            instance WS_Special, so the "WS_Standar d" in my line needs to be
            replaced with the name that I put in the arguments of the calling
            function, showDetail('WS_ Special','1').

            (Otherwise I would have to duplicate the stupid function as many times
            as I have Arrays)

            I've tried different things such as

            document.getEle mentById('tdDes c').innerHTML=t ypeDesc[noDesc];
            document.getEle mentById('tdDes c').innerHTML=s howDesc.argumen t[0][noDesc]
            ;

            and so on, but it either resulted in an error, or I got just portions of
            the name itself as in

            showDetail('WS_ Standard','0') returned 'W'
            showDetail('WS_ Standard','1') returned 'S'
            showDetail('WS_ Standard','2') returned '_'

            and so on...

            Plain and simple: When the function called goes
            showDetail('WS_ Special','1');

            what do I have to write here
            document.getEle mentById('tdDes c').innerHTML=? ??[noDesc];

            so that the function fills my tdDesc-Tablecell with the value of the
            Array "WS_Special[1]"?

            You help is highly appreciated!

            Martin

            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            • Martin

              #7
              Re: Addressing an Array from Function Arguments?

              Hi Robert,

              I just reread your revised code sample. As a matter of fact, when I use
              this, there's another error coming up:

              You're calling onload "resetDesc('WS_ Standard',WS_St andard.length') "

              WS_Standard.len gth returns 10, because "WS_Standar d" consists of 11
              characters. Here too, the string itself is addressed and not the Array
              with that name. Why's that? Seems similiar to my particular problem...

              Regards,

              Martin

              *** Sent via Developersdex http://www.developersdex.com ***
              Don't just participate in USENET...get rewarded for it!

              Comment

              • RobG

                #8
                Re: Addressing an Array from Function Arguments?

                Martin Altschwager wrote:[color=blue]
                > Hi,
                >
                > I desperately need some help with what I assume to be a minor problem
                > that I just can't solve.
                >
                > I'm writing a page where on mouseover a link, an image needs to be
                > displayed and some text, which is defined inside an array, sould
                > appear inside a table cell (whose id is "tdDesc").
                >
                > That's basically what's in the HTML-file:
                >
                > <body onload="resetDe sc('WS_Standard ','3');">
                > <a href="#" onmouseover="sh owDesc('WS_Stan dard','1');"
                > onmouseover="sh owDesc('WS_Stan dard','1');">
                > Link no. 1</a>[/color]

                Since you creating your messages as a global array, why not pass
                them directly?

                <a href="#" onmouseover="sh owDesc(WS_Stand ard[1]);"
                onmouseout="sho wDesc(WS_Standa rd[0]);">


                [...][color=blue]
                > 18 var WS_Standard = new Array();
                > 19 WS_Standard[0] = "Standard text when no mouse over";
                > 20 WS_Standard[1] = "Sample Text 1";
                > 21 WS_Standard[2] = "Sample Text 2";
                > 22 WS_Standard[3] = "Sample Text 3";[/color]


                This is simpler (to me) when written as:

                var WS_Standard = [
                "Standard text when no mouse over",
                "Sample Text 1",
                "Sample Text 2",
                "Sample Text 3"
                ];

                Both create exactly the same array.


                --
                Rob

                Comment

                • Martin

                  #9
                  Re: Addressing an Array from Function Arguments?

                  RobG wrote:[color=blue]
                  >[color=green]
                  > > That's basically what's in the HTML-file:
                  > >
                  > > <body onload="resetDe sc('WS_Standard ','3');"
                  > > <a href="#"
                  > > onmouseover="sh owDesc('WS_Stan dard','1');"
                  > > onmouseover="sh owDesc('WS_Stan dard','1');"
                  > > Link no. 1</a>[/color]
                  >
                  > Since you creating your messages as a global array, why
                  > not pass them directly?
                  >
                  > <a href="#" onmouseover="sh owDesc(WS_Stand ard[1]);"
                  > onmouseout="sho wDesc(WS_Standa rd[0]);">[/color]

                  Thanks for answering. It's a good idea, however, that same function must
                  show or hide the corresponding image, too.

                  My image are called "imgWS_Standard _1" and so on (compare lines 2 and 4,
                  7 and 9, 13 and 14). I feel there must be an easier way than using the
                  argument "WS_Standar d[1]" and then splitting it up again to make it
                  "imgWS_Standard "...

                  Any idea?
                  [color=blue]
                  > This is simpler (to me) when written as:
                  >
                  > var WS_Standard = [
                  > "Standard text when no mouse over",
                  > "Sample Text 1",
                  > "Sample Text 2",
                  > "Sample Text 3"
                  > ];
                  >
                  > Both create exactly the same array.[/color]

                  Maybe, but I like the numbers nearby so that I know instantly what text
                  corresponds to what number (think 20 entries). I believe it makes no
                  difference here...

                  Thanks!

                  Martin

                  *** Sent via Developersdex http://www.developersdex.com ***
                  Don't just participate in USENET...get rewarded for it!

                  Comment

                  • RobG

                    #10
                    Re: Addressing an Array from Function Arguments?

                    Martin wrote:[color=blue]
                    > RobG wrote:[/color]
                    [...][color=blue][color=green]
                    >>Since you creating your messages as a global array, why
                    >>not pass them directly?
                    >>
                    >> <a href="#" onmouseover="sh owDesc(WS_Stand ard[1]);"
                    >> onmouseout="sho wDesc(WS_Standa rd[0]);">[/color]
                    >
                    >
                    > Thanks for answering. It's a good idea, however, that same function must
                    > show or hide the corresponding image, too.[/color]

                    OK, you can still pass more parameters:

                    <a href="#" onmouseover="sh owDesc(WS_Stand ard[1],'1');"
                    onmouseout="sho wDesc(WS_Standa rd[0],'2');">

                    WS_Standard[1] will be evaluated so that your function
                    showDesc() gets the string (or whatever is in WS_Standard).
                    [color=blue]
                    >
                    > My image are called "imgWS_Standard _1" and so on (compare lines 2 and 4,
                    > 7 and 9, 13 and 14). I feel there must be an easier way than using the
                    > argument "WS_Standar d[1]" and then splitting it up again to make it
                    > "imgWS_Standard "...
                    >
                    > Any idea?
                    >
                    >[color=green]
                    >> This is simpler (to me) when written as:
                    >>
                    >> var WS_Standard = [
                    >> "Standard text when no mouse over",
                    >> "Sample Text 1",
                    >> "Sample Text 2",
                    >> "Sample Text 3"
                    >> ];
                    >>
                    >> Both create exactly the same array.[/color]
                    >
                    >
                    > Maybe, but I like the numbers nearby so that I know instantly what text
                    > corresponds to what number (think 20 entries). I believe it makes no
                    > difference here...[/color]

                    Fine, just making a suggestion.

                    Here's another. You can create an object that contains your
                    arrays, then you can address them using strings passed to your
                    function (I think this fits better with what you are trying to
                    do). They are often incorrectly called "hashtables " or
                    "2D arrays":


                    <html><head>
                    <title>2d arrays</title>
                    <script type="text/javascript">

                    var WS_Standard = ["Standard text when no mouse over",
                    "Standard Text 1","Standard Text 2","Standard Text 3"];


                    var WS_Super = ["Super text when no mouse over",
                    "Super Text 1","Super Text 2","Super Text 3"];

                    var msgObj = {'WS_Standard': WS_Standard,'WS _Super':WS_Supe r};


                    function zap(aName,aInde x) {
                    return(msgObj[aName][aIndex]);
                    }

                    </script>
                    </head>
                    <body>


                    <input type="button" value="std 0" onclick="
                    alert(zap('WS_S tandard','0'));
                    ">
                    <input type="button" value="std 1" onclick="
                    alert(zap('WS_S tandard','1'));
                    ">
                    <input type="button" value="std 2" onclick="
                    alert(zap('WS_S tandard','2'));
                    ">
                    <input type="button" value="std 3 onclick="
                    alert(zap('WS_S tandard','3'));
                    ">
                    <br>
                    <input type="button" value="super 0" onclick="
                    alert(zap('WS_S uper','0'));
                    ">
                    <input type="button" value="super 1" onclick="
                    alert(zap('WS_S uper','1'));
                    ">
                    <input type="button" value="super 2" onclick="
                    alert(zap('WS_S uper','2'));
                    ">
                    <input type="button" value="super 3" onclick="
                    alert(zap('WS_S uper','3'));
                    ">
                    </body></html>


                    The above is just play code to demonstrate a "2d" array.



                    --
                    Rob

                    Comment

                    • Stephen Chalmers

                      #11
                      Re: Addressing an Array from Function Arguments?

                      Martin Altschwager <cosas@altschwa ger.de> wrote in message >[color=blue]
                      > Hi Stephen,
                      >
                      > 01 function showDesc(typeDe sc,noDesc) {
                      > 03 document.getEle mentById('tdDes c').innerHTML = typeDesc[noDesc];
                      >
                      > These two lines obviously don't work. What's wrong?
                      >
                      > Thank you already for further help!
                      >
                      > Martin[/color]

                      I wish you had shown that code initially. In that case the problem is still
                      in the way you are passing the relevant parameter, assuming this function
                      call is unchanged:

                      <a href="#" onmouseover="sh owDesc('WS_Stan dard','1');"

                      which means that you end up trying to subscript the string typeDesc.

                      --
                      S.C.





                      Comment

                      • Martin

                        #12
                        Re: Addressing an Array from Function Arguments?

                        Hi Rob,

                        you're a genius and you were right. It was exactly the
                        what-should-not-be-called-2D-arrays solution that I needed. (I had heard
                        of this, but never learnt how to do it.) I had it implemented within a
                        minute and it works perfectly.

                        Thank you very much!

                        Martin

                        *** Sent via Developersdex http://www.developersdex.com ***
                        Don't just participate in USENET...get rewarded for it!

                        Comment

                        • Martin Altschwager

                          #13
                          Re: Addressing an Array from Function Arguments?

                          "Stephen Chalmers" <ignoring@lycos .co.uk> wrote in message news:<42080877$ 1_3@mk-nntp-2.news.uk.tisca li.com>...[color=blue]
                          > Martin Altschwager <cosas@altschwa ger.de> wrote in message
                          > news:bbebb69b.0 502071410.25ca1 280@posting.goo gle.com...
                          >[color=green]
                          > > onmouseover="sh owDesc('WS_Stan dard','1');"[/color]
                          >[color=green]
                          > > 01 function showDesc(typeDe sc,noDesc)[/color]
                          > {
                          >[color=green]
                          > >03 document.getEle mentById('tdDes c').innerHTML = WS_Standard[noDesc];[/color]
                          >
                          > You're subscripting the array with the string: '1';[/color]

                          My problem's been solved. I'm using 2D-Arrays now.

                          Thanks!

                          Martin

                          Comment

                          • Robert

                            #14
                            Re: Addressing an Array from Function Arguments?

                            In article <4208afa3$1_1@1 27.0.0.1>, Martin <no.spam@for.me > wrote:
                            [color=blue]
                            >
                            > I'm explicitly pointing to the Array WS_Standard, and that works
                            > perfectly.
                            >
                            > However, I want to use the function later again with another Array, for
                            > instance WS_Special, so the "WS_Standar d" in my line needs to be
                            > replaced with the name that I put in the arguments of the calling
                            > function, showDetail('WS_ Special','1').
                            >
                            > (Otherwise I would have to duplicate the stupid function as many times
                            > as I have Arrays)[/color]

                            There are two things you are working with here. One is the array
                            WS_Standard and the other is the string of characters used in the id
                            attribute of the img tag. You have given them the similar names which
                            is confusing.

                            Here is a way of accessing multiple lists and images and text.

                            Robert

                            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                            "http://www.w3.org/TR/html4/loose.dtd">
                            <html>
                            <head>
                            <title>Saving stats</title>
                            <base href="http://spaceplace.jpl. nasa.gov/en/">
                            <script type="text/javascript">

                            /* I am create an object grouptArrays which contains the indexes
                            WS_Standard and WS_Special. Each index points to an arrary
                            with the desired text strings. I put in comments to note
                            the index in the array.

                            You can use the bracket notaion [ ] to access objects.

                            Trying to initialize lots of data via assignment statements
                            will lead to errors when you copy data to save typing and
                            when you change entries. */

                            /* I find your variable names confusing. noDesc could be no description
                            instead of number description. Arrarys take indexes so using index
                            over
                            number has more meaning.
                            */

                            var groupArray = {'WS_Standard': [
                            /* 0 */ "Standard text when no mouse over",
                            /* 1 */ "Sample Text 1",
                            /* 2 */ "Sample Text 2",
                            /* 3 */ "Sample Text 3"
                            ],
                            'WS_Special': [
                            /* 0 */ "special text when no mouse over",
                            /* 1 */ "special Text 1",
                            /* 2 */ "special Text 2",
                            /* 3 */ "special Text 3"
                            ]
                            }


                            function showDesc(descri ption,theIndex) {
                            var imgDesc = ('img' + description + '_' + theIndex);
                            document.getEle mentById('td_'+ description).in nerHTML =
                            groupArray[description][theIndex];
                            document.getEle mentById(imgDes c).style.displa y = "";
                            }


                            function hideDesc(descri ption,theIndex) {
                            var imgDesc = ('img' + description + '_' + theIndex);
                            document.getEle mentById('td_'+ description).in nerHTML =
                            groupArray[description][0];
                            document.getEle mentById(imgDes c).style.displa y = "none";
                            }

                            function resetDesc(descr iption,totalDes c) {
                            for (var i = 1; i < totalDesc; i++) {
                            var imgDesc = ('img' + description + '_' + i);
                            document.getEle mentById(imgDes c).style.displa y = "none";
                            }
                            document.getEle mentById('td_'+ description).in nerHTML =
                            groupArray[description][0];
                            }


                            </script>
                            </head>
                            <body onload="resetDe sc('WS_Standard ',groupArray['WS_Standard'].length);
                            resetDesc('WS_S pecial',groupAr ray['WS_Special'].length);">

                            <a href="#" onmouseover="sh owDesc('WS_Stan dard',1);"
                            onmouseout="hid eDesc('WS_Stand ard',1);">
                            Link number 1</a>

                            <p>
                            <img
                            id="imgWS_Stand ard_1"
                            src="_images/common/nasa_header/logo_nasa.gif">
                            <br>
                            <img
                            id="imgWS_Stand ard_2"
                            src="_images/common/nasa_header/logo_nasa.gif">
                            <br>
                            <img
                            id="imgWS_Stand ard_3"
                            src="_images/common/nasa_header/logo_nasa.gif">
                            </p>
                            <p>
                            <span id="td_WS_Stand ard" class="hidden"> Message to
                            display</span>
                            </p>


                            <a href="#" onmouseover="sh owDesc('WS_Spec ial',1);"
                            onmouseout="hid eDesc('WS_Speci al',1);">
                            Link number 2</a>

                            <p>
                            <img
                            id="imgWS_Speci al_1"
                            src="educators/images/solarsystem/clem_full_moon_ T.jpg">
                            <br>
                            <img
                            id="imgWS_Speci al_2"
                            src="educators/images/solarsystem/clem_full_moon_ T.jpg">
                            <br>
                            <img
                            id="imgWS_Speci al_3"
                            src="educators/images/solarsystem/clem_full_moon_ T.jpg">
                            </p>
                            <p>
                            <span id="td_WS_Speci al" class="hidden"> Message to
                            display</span>
                            </p>
                            </body>
                            </html>

                            Comment

                            Working...