SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    acces an Array with data from getElementById

    <script>

    var arrayOne=('onehorse','onebird')
    var arrayTwo=('twohorses','2birds')
    var arrayThree=('3horses','3birds')

    function doSome(idArray) {

    alert(idArray[1])
    }

    </script>
    <body onload=doSome('arrayTwo')>


    Does not work. I would expect '2birds' but returns undefined. Probably because the typeof idArray I get from the function call is string. What is the right way to do it?

  2. #2
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry not getElementById as I wrote in the headline above

  3. #3
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your arrays are global variables, like in your example,
    Code JavaScript:
    function doSome(idArray) {
      alert(window[idArray][1]);
    }
    Birnam wood is come to Dunsinane

  4. #4
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the problem is solved. I must have been too tired (headache). The function call must be doSome(arrayTwo) instead of doSome('arrayTwo'). Just so used to the importance of remenbering ' '. Thanks for your reply anyway

  5. #5
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that's even easier.
    I thought you were forced to use a string value, for some reason.
    Birnam wood is come to Dunsinane


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •