Filling a select menu from associative array

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

    Filling a select menu from associative array

    Hello all,

    on a web page, i use php to create several associative arrays in javascript:

    subTab1 = new Object();
    subTab1["key1"] = "value1";

    subTab2 = new Object();
    subTab2["key1"] = "value1";


    and i have two select menu:

    <select name="selectMen u1" onChange="javas cript:type_chan ged()"

  • david

    #2
    Re: Filling a select menu from associative array

    fucking mozilla, my post got cut, sorry

    Hello all,

    on a web page, i use php to create several associative arrays in javascript:

    subTab1 = new Object();
    subTab1["key1"] = "value1";

    subTab2 = new Object();
    subTab2["key1"] = "value1";


    and i have two select menu:

    <select name="selectMen u1" onChange="javas cript:type_chan ged()">
    <option value="subTab1" >Category 1</option>
    <option value="subTab2" >Category 2</option>
    </select>

    <select name="selectMen u2">
    </select>

    and i have the following function in "almost" code ;) :

    function type_changed()
    {
    type = document.theFor m.selectMenu1.v alue;

    // object name to object
    tab = document[type];

    dst = document.theFor m.selectMenu2;

    // clear the menu
    for (var i = 0; i < dst.length; i++)
    dst.options[i] = null;

    for (var i = 0; i < tab.length; i++)
    {
    dst.options[i] = new Option(tab.valu e, tab.key);
    }
    }

    If somebody could help me to finalyse the function :)

    Thx by advance

    --
    David

    Comment

    • david

      #3
      Re: Filling a select menu from associative array

      david wrote:

      it's done

      function type_changed()
      {
      var type = document.theFor m.selectMenu1.v alue;

      // object name to object
      var tab = g[type];

      var dst = document.theFor m.selectMenu2;

      // clear the menu
      for (var i = 0; i < dst.length; i++)
      dst.options[i] = null;

      i = 0;
      for (key in tab)
      {
      dst.options[i] = new Option(tab[key], key);
      i++;
      }
      }

      --
      David

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Filling a select menu from associative array

        david <pas.d.email@se rver.com> writes:
        [color=blue]
        > david wrote:
        >
        > it's done[/color]
        ....[color=blue]
        > var dst = document.theFor m.selectMenu2;[/color]

        I recommend
        var dst = document.forms['theForm'].elements['selectMenu2'];
        but that is mostly a matter of taste.
        [color=blue]
        > // clear the menu
        > for (var i = 0; i < dst.length; i++)
        > dst.options[i] = null;[/color]

        This clearing fails. Try insterting an
        alert(dst.lengt h);
        after the loop and see that approx. half of the options are still
        there. This is because setting an option to null will remove it,
        which moves the next option into its place and reduces the length.

        A better solution is
        dst.length = 0;

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        Working...