Results 1 to 5 of 5

Thread: How to populate and use an array in Javascript?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    How to populate and use an array in Javascript?

    I need to create an array in Javascript in my web page that contains 3 columns:

    CategoryID
    ReasonID
    Value

    So for example, I need an array to have the following info:

    1,1,Value1
    1,2,Value2
    1,3,Value3
    2,1,Value1
    2,2,Value2
    2,3,Value3

    I will then have 2 comboboxes on my web page. The first combobox will simply contain a CategoryID value. The second combobox needs to be populated based on which CategoryID is selected in combobox 1. So for example, if CategoryID 2 is selected in combobox 1, I need combobox 2 to populated with all values from the array that contain a 2 as the first element.

    An example of how to do this would be greatly appreciated.

    Visual Studio 2010

  2. #2
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    I'd use objects for this

    Code:
    // First declare the object type, can be done with a function
    function ObjectName(categoryID, reasonID, value)
    {
      this.categoryID = categoryID;
      this.reasonID = reasonID;
      this.value = value;
    }
    
    // Then declare the array
    var myArray = new Array();
    
    // Make an event handler which fills the combo boxes, place it in the body tag, like: <body onload="init()">
    function init()
    {
      // Fill the array
      myArray.push(
        new ObjectName(1, 1, "Value1"),
        new ObjectName(1, 2, "Value2"),
        ...
        new ObjectName(2, 3, "Value3"));
    
      // Loop through the array and fill the combobox
      var myCombo = document.getElementById("comboId")';
      for(var i = 0; i < myArray.length; i++)
      {
        myCombo.add(new Option(myArray[i].categoryID, myArray[i].categoryID, false, false);
      }
    
      return;
    
    }
    I hope you can do the rest from here, like filling the second combo box when the user selects a value from the first. If not just put your questions here.
    You might read a bit about event handling in JavaScript. You can put an event on a select box the same way as I did on the body tag. Use the onclick event for this purpose.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Seems to be erroring out on the following line:

    myCombo.add(new Option(myArray[i].categoryID, myArray[i].categoryID, false, false);

    Any ideas?

    Visual Studio 2010

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Nevermind, I see that a parenthesis was missing at the end...

    Visual Studio 2010

  5. #5
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    Originally posted by dbassettt74
    Nevermind, I see that a parenthesis was missing at the end...
    Yup, my mistake, didn't test it

Posting Permissions

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



Click Here to Expand Forum to Full Width