www.webdeveloper.com
Results 1 to 3 of 3

Thread: JavaScript: How to Get the Values from a Multi-Dimensional Array?

Hybrid View

  1. #1
    Join Date
    Apr 2012
    Posts
    1

    JavaScript: How to Get the Values from a Multi-Dimensional Array?

    wondering how to get the values from a multi-dimensional array? This is what I have so far:

    <script type="text/javascript">
    var concertArray = [
    ["Billy Joel", "99", "equal.png"],
    ["Bryan Adams", "89", "higher.png"],
    ["Brian Adams", "25", "lower.png"]
    ];

    function populate(){

    for(i=0;i<concertArray.length;i++){
    var select = document.getElementById("test");
    select.options[select.options.length] = new Option(concertArray[i][0], concertArray[i][1]);
    }

    }
    </script>

    Now, does anyone know how to get the value of 99 and the image when I select the first option in the array, "Billy Joel"?

  2. #2
    Join Date
    Nov 2010
    Posts
    1,097
    html:
    Code:
    <select id="test" onchange="showPick(this.selectedIndex)">
    </select>
    js:
    Code:
    function showPick(idx){
    alert(concertArray[idx][1]+ " "+concertArray[idx][2])
    }

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434

    Lightbulb

    Good start, but since the OP has not learned to use the [ code] and [/ code] tags (without spaces)
    I thought they might need a bit more to get an actual display.
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title> Untitled </title>
    <script type="text/javascript">
    var concertArray = [
       ['Select artist','',''],
       ["Billy Joel", "99", "equal.png"],
       ["Bryan Adams", "89", "higher.png"],
       ["Brian Adams", "25", "lower.png"]
    ];
    
    function populate() {
      for(i=0;i<concertArray.length;i++){
        var select = document.getElementById("test");
        select.options[select.options.length] = new Option(concertArray[i][0], concertArray[i][1]);
      }
    }
    
    function showPick(idx){
      if (idx == 0) { return; }
      alert(concertArray[idx][0]+" "+concertArray[idx][1]+ " "+concertArray[idx][2]);
    }
    window.onload = function() {
      populate();
    }
    </script>
    </head>
    <body>
    <select id="test" onchange="showPick(this.selectedIndex)">
    </select>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Posting Permissions

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



Recent Articles