Form is set by me

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

    Form is set by me

    I have a drop-down box. I also have a var with a value. I want the
    value in the var to be displayed in the drop-down box. How can I do
    this?

    Thanks.

    Scott
  • Bart Van der Donck

    #2
    Re: Form is set by me

    Scott wrote:
    [color=blue]
    > I have a drop-down box. I also have a var with a value. I want the
    > value in the var to be displayed in the drop-down box. How can I do
    > this?[/color]

    How should your var behave in regard to your dropdownbox? Is your
    dropdownbox already populated with <option>'s, before the code is
    invoked that would alter its content? Should the dropdownbox react on
    events on the webpage to get new content?

    Reading your question as it is stated, this basic code could be an
    answer:

    var myvar = "somevalue" ;
    document.write( '<select size=1 name=myselectbo x>');
    document.write( '<option value="'+myvar+ '">'+myvar+' </option>');
    document.write( '</select>');

    Bart

    Comment

    • Scottie

      #3
      Re: Form is set by me

      Bart,
      [color=blue]
      > How should your var behave in regard to your dropdownbox? Is your
      > dropdownbox already populated with <option>'s, before the code is
      > invoked that would alter its content? Should the dropdownbox react on
      > events on the webpage to get new content?[/color]

      I didn't get my message clear enough. My var already has a value. My
      dropdownbox is already populated with <options>'s. What I want to know
      is can my var be matched with the <option>'s directly and the <option>
      get executed.

      Scott

      Comment

      • Bart Van der Donck

        #4
        Re: Form is set by me

        > I didn't get my message clear enough. My var already has a value. My[color=blue]
        > dropdownbox is already populated with <options>'s. What I want to know
        > is can my var be matched with the <option>'s directly and the <option>
        > get executed.[/color]

        Well you still left a few things unspecified, but guessing at those,
        this code should do the trick:

        <html>
        <head>
        <!-- var already has value, I'm setting it here -->
        <script language="javas cript">
        var myvar = "somevalue" ;
        </script>
        </head>
        <body>
        <!-- your dropdownbox with its options, set in html -->
        <form action="somescr ipt.cgi">
        <select size="1" name="mybox">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        </select>
        </form>
        <!-- now change contents of the box -->
        <script language="javas cript">
        // empty the box
        while (document.forms[0].mybox.options. length)
        document.forms[0].mybox.options[0] = null;
        // put var in the box
        document.forms[0].mybox.options[document.forms[0].mybox.length]
        = new Option(myvar, myvar);
        // now set which option must be selected.
        // required in NS4, and handy in IE when you want
        // to select an option that doesn't come first
        document.forms[0].mybox.selected Index = 0;
        // now "execute" the dropdownbox: do you mean submitting the form?
        // if so, uncomment this line:
        // document.forms[0].submit();
        </script>
        </body>
        </html>

        Bart

        Comment

        • Scottie

          #5
          Re: Form is set by me

          Bart,

          I came up with this. It works:

          var str = window.location .search; // returns
          '?text=language ' from the URL
          if (str != "") {
          var temp = str.split('=');
          for (i=1; i <= speclang.length ; i++) { // goes through whole
          language array
          if (temp[1] == speclang[i]) break; // found
          }
          if (i > speclang.length ) { // didn't find
          alert(temp[1] + " was not found in the Mexican language list.");
          return;
          }
          }
          document.forms['Language'].selectname.sel ectedIndex = i; // make
          dropdown box = passed language
          languag(i); // update to specific language
          }

          Comment

          Working...