Multidimensional arrays in Javascript

Posted on 2007-08-02
Last Modified: 2008-01-09
I've posted some code for a simple form.  I have commented out the part of the function that isn't working as I would expect.

When a 'country' is selected from the drop-down menu, the corresponding 'country code' is populated in text fields of the form.

This works (form rows '1' and '2'):
  document.forms.myForm.country0.value = countryCode

This does not work (form rows '3' and '4'):
  document.forms.myForm.people[0][country].value = countryCode

I'd prefer to reference the form elements as in rows 3 and 4 but am uncertain how to reference multidimensional arrays in Javascript.

How should I go about this?

<title>People :: Names and Countries</title>
<script type="text/javascript">
  var country = "";
  function addCountry(countryCode) {

    // This works -- 'country code' IS ADDED to rows 1 and 2
    document.forms.myForm.country0.value = countryCode
    document.forms.myForm.country1.value = countryCode
    // This doesn't work -- 'country code' IS NOT ADDED to rows 3 and 4
    // document.forms.myForm.people[0][country].value = countryCode
    // document.forms.myForm.people[1][country].value = countryCode

<form name="myForm" method="post" action="processForm.php">
  <select name="countryCode" onchange="addCountry(this.value);">
    <option value="" selected>(Select COUNTRY)</option>
    <option value="CAN">Canada</option>
    <option value="FRA">France</option>
    <option value="UK">United Kingdom</option>

1. NAME:<input type=text name=name0> COUNTRY:<input type=text name=country0><br />
2. NAME:<input type=text name=name1> COUNTRY:<input type=text name=country1><br />

3. NAME:<input type=text name=people[0][name]> COUNTRY:<input type=text name=people[0][country]><br />
4. NAME:<input type=text name=people[1][name]> COUNTRY:<input type=text name=people[1][country]><br />
Question by:speyfisher
    LVL 54

    Accepted Solution

    I don't see an array, or at least a Javascript array.  Fields 3 and 4 have a name that uses a bracket but that doesn't make it a Javascript array.  The brackets may cause problems for Javascript to access it but try ...

        document.forms.myForm.elements["people[0][country]"].value = countryCode
        document.forms.myForm.elements["people[1][country]"].value = countryCode

    Let me know how that works or if you get an error.

    LVL 54

    Expert Comment

    If that doesn't work then can you either put the name value in quotes (e.g. name="people[0][name]") or use a different name (one without brackets)?

    LVL 4

    Author Comment

    The first comment got it going by using '.elements[]'

        document.forms.myForm.elements["people[0][country]"].value = countryCode

    The second comment-- (var name="...)  value in quotes didn't do it.  Using a different name without brackets worked fine in the example but I'd prefer the POST data sent to the php script be accessible something like: $country = $_POST['people'][$i]['country'];

    Thanks bol!
    LVL 54

    Expert Comment

    Your welcome!  I'm glad the first worked then.  I have had issues before but I was pretty confident elements[] would be a way to do it.

    Thanks for the grade, the points and the fun question.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
    Article by: DanRollins
    This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now