Dynamic populate drop down menu

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

    #1

    Dynamic populate drop down menu

    I am attempting to populate a drop down menu based on the selection of
    a different drop down menu. However, it is not working correctly, I
    cannot figure out for the life of me what exactly happens because I am
    not getting any errors on the page.

    <html>
    <script language="javas cript">
    var phaseArray = new phaseArray(4);
    var phaseTypeId = new phaseTypeId(4);
    var phaseId = new phaseId(4);

    phaseArray[0] = "Define";
    phaseId[0] = "phs_2003000000 01";
    phaseTypeId[0] = "pht_2003000000 01";

    phaseArray[1] = "Measure";
    phaseId[1] = "phs_2003000000 02";
    phaseTypeId[1] = "pht_2003000000 01";


    phaseArray[2] = "Analyze";
    phaseId[2] = "phs_2003000000 03";
    phaseTypeId[2] = "pht_2003000000 01";

    phaseArray[3] = "Project Start-Up";
    phaseId[3] = "phs_2003000000 06";
    phaseTypeId[3] = "pht_2003000000 02";


    function populateMenu(s) {
    //document.write( phastTypeId[1]);
    //document.write( s);

    var i=0,j=1;
    document.overvi ew.phase.option s.length = 1; //delete current
    options
    document.overvi ew.phase.option s[0].selected = true;
    for (var i=0; i<phaseArray.le ngth; i++){
    if(s == phaseTypeId[i]) {
    document.overvi ew.phase.option s.length = j;
    document.overvi ew.phase.option s[j] = new Option(phaseArr ay[i]);
    document.overvi ew.phase.option s[j].value = phaseId[i];
    //if( == phaseArray[i])
    // document.search Form.metroID2.o ptions[j].selected = true;
    j++;
    }
    }
    }

    </script>
    <body>

    <form name="overview" id="overview" action="test2.j sp"
    method="post">
    <select name="phaseType "
    onChange="popul ateMenu(documen t.overview.phas eType.options[document.overvi ew.phaseType.op tions.selectedI ndex].value);">
    <option value="-1">- select phase -</option>
    <option value="pht_2003 00000001">Phase 1</option>
    <option value="pht_2003 00000002">Phase 2</option>
    </select><br />
    <select name="phase" id="phase" style="width:10 0px;">
    <option></option>
    </select>
    </form>
    </body>
    </html>

    There maybe a simple solution to this. I am not extremely familiar
    with Javascript and may have made some sort of syntax error. Thanks
    all.
  • Chris

    #2
    Re: Dynamic populate drop down menu


    "Greg Scharlemann" <greg@dreamatla ntic.com> wrote in message
    news:812361ab.0 312090809.1e5eb 368@posting.goo gle.com...[color=blue]
    > I am attempting to populate a drop down menu based on the selection of
    > a different drop down menu. However, it is not working correctly, I
    > cannot figure out for the life of me what exactly happens because I am
    > not getting any errors on the page.[/color]

    I'm not sure you've got Javascript errors switched on then, cause I got
    errors on the first line of your script.
    Try replacing your array declarations with:
    var phaseArray = Array(4);
    var phaseTypeId = Array(4);
    var phaseId = Array(4);

    Otherwise it seems to be doing what you want it to do.
    [color=blue]
    >
    > <html>
    > <script language="javas cript">
    > var phaseArray = new phaseArray(4);
    > var phaseTypeId = new phaseTypeId(4);
    > var phaseId = new phaseId(4);
    >
    > phaseArray[0] = "Define";
    > phaseId[0] = "phs_2003000000 01";
    > phaseTypeId[0] = "pht_2003000000 01";
    >
    > phaseArray[1] = "Measure";
    > phaseId[1] = "phs_2003000000 02";
    > phaseTypeId[1] = "pht_2003000000 01";
    >
    >
    > phaseArray[2] = "Analyze";
    > phaseId[2] = "phs_2003000000 03";
    > phaseTypeId[2] = "pht_2003000000 01";
    >
    > phaseArray[3] = "Project Start-Up";
    > phaseId[3] = "phs_2003000000 06";
    > phaseTypeId[3] = "pht_2003000000 02";
    >
    >
    > function populateMenu(s) {
    > //document.write( phastTypeId[1]);
    > //document.write( s);
    >
    > var i=0,j=1;
    > document.overvi ew.phase.option s.length = 1; //delete current
    > options
    > document.overvi ew.phase.option s[0].selected = true;
    > for (var i=0; i<phaseArray.le ngth; i++){
    > if(s == phaseTypeId[i]) {
    > document.overvi ew.phase.option s.length = j;
    > document.overvi ew.phase.option s[j] = new Option(phaseArr ay[i]);
    > document.overvi ew.phase.option s[j].value = phaseId[i];
    > //if( == phaseArray[i])
    > // document.search Form.metroID2.o ptions[j].selected = true;
    > j++;
    > }
    > }
    > }
    >
    > </script>
    > <body>
    >
    > <form name="overview" id="overview" action="test2.j sp"
    > method="post">
    > <select name="phaseType "
    >[/color]
    onChange="popul ateMenu(documen t.overview.phas eType.options[document.overvi ew
    ..phaseType.opt ions.selectedIn dex].value);">[color=blue]
    > <option value="-1">- select phase -</option>
    > <option value="pht_2003 00000001">Phase 1</option>
    > <option value="pht_2003 00000002">Phase 2</option>
    > </select><br />
    > <select name="phase" id="phase" style="width:10 0px;">
    > <option></option>
    > </select>
    > </form>
    > </body>
    > </html>
    >
    > There maybe a simple solution to this. I am not extremely familiar
    > with Javascript and may have made some sort of syntax error. Thanks
    > all.[/color]


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Dynamic populate drop down menu

      greg@dreamatlan tic.com (Greg Scharlemann) writes:
      [color=blue]
      > I am attempting to populate a drop down menu based on the selection of
      > a different drop down menu. However, it is not working correctly, I
      > cannot figure out for the life of me what exactly happens because I am
      > not getting any errors on the page.[/color]

      That's surpricing. Are you sure you have enabled error messages?

      Remeber the DOCTYPE declaration (required in HTML).
      [color=blue]
      > <html>[/color]

      Remember the <head> tag.
      Remember the <title> element (required in HTML)
      [color=blue]
      > <script language="javas cript">[/color]

      <script type="text/javascript">
      The type attribute is required, and is also sufficient.
      [color=blue]
      > var phaseArray = new phaseArray(4);[/color]

      You probably mean
      var phaseArray = new Array(4);
      The above gives the error that "phaseArray is not a function" (because
      it is undefined as any unassigned variable).
      [color=blue]
      > var phaseTypeId = new phaseTypeId(4);
      > var phaseId = new phaseId(4);[/color]

      Ditto ditto.
      [color=blue]
      > function populateMenu(s) {
      > //document.write( phastTypeId[1]);[/color]
      [color=blue]
      > //document.write( s);
      >
      > var i=0,j=1;
      > document.overvi ew.phase.option s.length = 1; //delete current options[/color]

      I recommend using the forms collection:

      document.forms['overview'].elements['phase'].options.length = 1;

      (you want to keep the first option, right?)[color=blue]
      > document.overvi ew.phase.option s[0].selected = true;[/color]
      [color=blue]
      > document.overvi ew.phase.option s.length = j;
      > document.overvi ew.phase.option s[j] = new Option(phaseArr ay[i]);
      > document.overvi ew.phase.option s[j].value = phaseId[i];[/color]

      These three lines can be written:
      document.....op tions[j] = new Option(phaseArr ay[i],phaseId[i]);
      Assigning to a non-existing option index automatically increases
      the length of the options collection.
      [color=blue]
      > There maybe a simple solution to this. I am not extremely familiar
      > with Javascript and may have made some sort of syntax error.[/color]

      I believe it is the problem with the arrays at the beginning.

      /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

      • Greg Scharlemann

        #4
        Re: Dynamic populate drop down menu

        The problem was with the array declaration, but i do like the idea of
        rewriting some of the function with the suggested syntax thanks.

        I have a new question now though. I want a item in the second drop
        down menu to be selected initially but its not working like I thought
        it would. I added an if statement in the function and then called the
        function with the value of the item that I want initially
        selected...but the drop down menu doesn't even get populated anymore.
        How do I get it to populate the second drop down menu on page load? I
        assume once that works, the setting the selected = true in the
        function for the second drop down menu will work as well? Thanks!

        Here is the code:
        *************** *************** *************** *************** **
        <html>
        <head>
        <script language="javas cript">
        var phaseArray = new Array(4);
        var phaseTypeId = new Array(4);
        var phaseId = new Array(4);

        phaseArray[0] = "Define";
        phaseId[0] = "phs_2003000000 01";
        phaseTypeId[0] = "pht_2003000000 01";

        phaseArray[1] = "Measure";
        phaseId[1] = "phs_2003000000 02";
        phaseTypeId[1] = "pht_2003000000 01";


        phaseArray[2] = "Analyze";
        phaseId[2] = "phs_2003000000 03";
        phaseTypeId[2] = "pht_2003000000 01";

        phaseArray[3] = "Project Start-Up";
        phaseId[3] = "phs_2003000000 06";
        phaseTypeId[3] = "pht_2003000000 02";


        function populateMenu(s) {
        var i=0,j=1;
        document.overvi ew.phase.option s.length = 1; //delete current
        options
        document.overvi ew.phase.option s[0].selected = true;
        for (var i=0; i<phaseArray.le ngth; i++){
        if(s == phaseTypeId[i]) {
        document.overvi ew.phase.option s.length = j;
        document.overvi ew.phase.option s[j] = new Option(phaseArr ay[i]);
        document.overvi ew.phase.option s[j].value = phaseId[i];
        if("phs_2003000 00006" == phaseId[i])
        document.overvi ew.phase.option s[j].selected = true;
        j++;
        }
        }
        }

        </script>
        </head>
        <body>

        <form name="overview" id="overview" action="test2.j sp"
        method="post">
        <select name="phaseType "
        onChange="popul ateMenu(documen t.overview.phas eType.options[document.overvi ew.phaseType.op tions.selectedI ndex].value);">
        <option value="pht_2003 00000001">Phase 1</option>
        <option selected value="pht_2003 00000002">Phase 2</option>
        </select><br />
        <select name="phase" id="phase" style="width:10 0px;">
        <option></option>
        </select>
        </form>
        <script>populat eMenu("phs_2003 00000006");</script>

        </body>
        </html>

        Comment

        • Michael Winter

          #5
          Re: Dynamic populate drop down menu

          Greg Scharlemann wrote on 10 Dec 2003 at Wed, 10 Dec 2003 20:02:46
          GMT:

          <snipped description>

          You should have a DOCTYPE declaration here. If you know how to write
          Strict HTML (which I suggest you learn if you don't), use:

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
          "http://www.w3.org/TR/html4/strict.dtd">

          ....otherwise.. .

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
          "http://www.w3.org/TR/html4/loose.dtd">
          [color=blue]
          > <html>
          > <head>
          > <script language="javas cript">[/color]

          Mr Nielsen suggested in his reply to your original post that you use
          the type attribute. I again recommend that you change the line above.
          The type attribute is /required/. It also makes the language
          attribute redundant.

          <script type="text/javascript">
          [color=blue]
          > var phaseArray = new Array(4);
          > var phaseTypeId = new Array(4);
          > var phaseId = new Array(4);
          >
          > phaseArray[0] = "Define";
          > phaseId[0] = "phs_2003000000 01";
          > phaseTypeId[0] = "pht_2003000000 01";
          >
          > phaseArray[1] = "Measure";
          > phaseId[1] = "phs_2003000000 02";
          > phaseTypeId[1] = "pht_2003000000 01";
          >
          >
          > phaseArray[2] = "Analyze";
          > phaseId[2] = "phs_2003000000 03";
          > phaseTypeId[2] = "pht_2003000000 01";
          >
          > phaseArray[3] = "Project Start-Up";
          > phaseId[3] = "phs_2003000000 06";
          > phaseTypeId[3] = "pht_2003000000 02";
          >
          >
          > function populateMenu(s) {
          > var i=0,j=1;
          > document.overvi ew.phase.option s.length = 1; //delete current
          > options
          > document.overvi ew.phase.option s[0].selected = true;[/color]

          You're using the wrong property. SELECT elements have a selectedIndex
          property that indicates the currently selected item. The selected
          property of OPTION elements (what you have above) is used to
          determine what entries are selected when SELECT is used as a list
          box, rather than a drop-down box.

          To make your script compatible with more browsers, you should change
          how you access form elements to the collection syntax:

          document.forms['overview'].elements['phase'].selectedIndex = 0;

          It is longer, but you could create a reference to the form, an
          element, or other object early on in the script. For example,

          var phase = document.forms['overview'].elements['phase'];

          phase.selectedI ndex = 0;
          [color=blue]
          > for (var i=0; i<phaseArray.le ngth; i++){
          > if(s == phaseTypeId[i]) {[/color]

          If the variable above is used, the code below could be shortened to:

          phase.options.l ength = j;
          phase.options[j] = new Option(phaseArr ay[i]);

          ....and so on.
          [color=blue]
          > document.overvi ew.phase.option s.length = j;
          > document.overvi ew.phase.option s[j] = new
          > Option(phaseArr ay[i]);
          > document.overvi ew.phase.option s[j].value = phaseId[i];
          > if("phs_2003000 00006" == phaseId[i])
          > document.overvi ew.phase.option s[j].selected = true;[/color]

          This should also be changed to use selectedIndex:

          document.forms['overview'].elements['phase'].selectedIndex = j;

          or, if you use the phase variable above:

          phase.selectedI ndex = j;
          [color=blue]
          > j++;
          > }
          > }
          > }
          >
          > </script>
          > </head>
          > <body>
          >
          > <form name="overview" id="overview" action="test2.j sp"
          > method="post">
          > <select name="phaseType "
          > onChange="popul ateMenu(documen t.overview.phas eType.options[docume
          > nt.overview.pha seType.options. selectedIndex].value);">
          > <option value="pht_2003 00000001">Phase 1</option>
          > <option selected value="pht_2003 00000002">Phase 2</option>
          > </select><br />
          > <select name="phase" id="phase" style="width:10 0px;">
          > <option></option>
          > </select>
          > </form>
          > <script>populat eMenu("phs_2003 00000006");</script>[/color]

          This script must have a type attribute like the one above.
          [color=blue]
          > </body>
          > </html>[/color]

          Hope that helps,

          Mike

          --
          Michael Winter
          M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk")

          Comment

          • @SM

            #6
            Re: Dynamic populate drop down menu

            Michael Winter a ecrit :
            [color=blue]
            > You're using the wrong property. SELECT elements have a selectedIndex
            > property that indicates the currently selected item. The selected
            > property of OPTION elements (what you have above) is used to
            > determine what entries are selected when SELECT is used as a list
            > box, rather than a drop-down box.[/color]

            ????
            when I do a select menu (no size)
            if I put the selected on third item
            this item appears in head (as title of select menu)

            It seems to work same if JS changes
            [color=blue]
            >[color=green]
            > >[/color]
            > Michael Winter
            > M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk")[/color]

            --
            *************** *************** *************** *************** **
            Stéphane MORIAUX : mailto:stephane OTER-MOImoriaux@wana doo.fr

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: Dynamic populate drop down menu

              @SM wrote:
              [color=blue]
              > Michael Winter a ecrit :[color=green]
              >> You're using the wrong property. SELECT elements have a
              >> selectedIndex property that indicates the currently selected item.[/color][/color]

              True if the `select' element is of type `select-one' where the
              `multiple' attribute is missing. Otherwise it returns the index
              of the first selected option in my Mozilla/5.0, IE and Opera.
              [color=blue][color=green]
              >> The selected property of OPTION elements (what you have above) is
              >> used to determine what entries are selected when SELECT is used as
              >> a list box, rather than a drop-down box.[/color][/color]

              Wrong. The `selected' property of an HTMLOptionEleme nt retrieves and
              sets the select status of an option no matter how the parent select
              element is displayed. The `selected' HTML attribute it corresponds to
              sets the respective option to selected by default. True is only that
              `select' elements *with* `multiple' attribute are displayed as a list
              box rather than a drop-down box in Mozilla/5.0, IE and Opera even if
              they have `size="1"'.
              [color=blue]
              > ????[/color]

              !!!!!!!11111111 11 [1]
              [color=blue]
              > It seems to work same if JS changes[/color]

              Selecting *one* option works this way with `select' elements of type
              `select-one'. If the type is `select-multiple', you get/set the select
              status of only one option, leaving the status of the other options
              unchanged.

              Since with type `select-one' `select' elements the additional lookup
              operation within the HTMLOptionsColl ection is inefficient, one should
              then access the `selectedIndex' property of the HTMLSelectEleme nt
              object instead.

              (Oh my, how often have I know written the words `select' or `selected'
              in this posting? ;-))
              [color=blue]
              > --[/color]

              ....


              HTH

              Pointed"select" Ears
              ___________
              [1] Questions do not become more questioning and exclamations do not
              become more exclamatory when multiplying the sentence marks.
              They only suck more when reading them.
              --

              Comment

              Working...