Web Analytics Made Easy -
StatCounter Please help with this form populate problem!!! - CodingForum

Announcement

Collapse
No announcement yet.

Please help with this form populate problem!!!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Please help with this form populate problem!!!

    I just want to set the select box value using a button like this

    <BUTTON onclick="document.forms[0].select1.value = 'a'">SET</Button>
    ...

    <select name="select1" size="1">
    <option>a
    <option>b
    <option>c
    </select>

    But I found when I click the button, the select box change to blank.
    And when I change the form to this:

    <select name="select1" size="1">
    <option value="a">a
    <option value="b">b
    <option value="c">c
    </select>

    It works!
    Could any body tell me why? and how to solve it
    thanks

  • #2
    1. document.forms[0].select1 is an array of options
    2. the content of an option is reurn by text, not by value (option is a different form's element compare with input).
    3. better use a function, or specify javascript: ()

    But:
    It is not very clear for me if you want change the option's focus or you want to change the option's text
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

    Comment


    • #3
      If you want to change the focused Index, you may use that:

      PHP Code:
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <
      html>
      <
      head>
      <
      script>
      function 
      bla(which){
      //build a new object
      myopt = new Option();
      //shortcut
      document.forms[0].select1;
      //search the array
      for(i=0;i<o.length;i++){
      //compare option.text with the parameter
      if (o.options[i].text == which){
      //give the new object a defined selectedIndex
      myopt.selectedIndex i;
      }
      }
      //set the desired selectedIndex
      o.selectedIndex myopt.selectedIndex;
      }
      </
      script>
      </
      head>

      <
      body>
      <
      form>
      <
      select name="select1" size="1">
          <
      option selected>...</option>
          <
      option>a</option>
          <
      option>b</option>
          <
      option>c</option>
        </
      select><br><br>
        <
      input type="button" onclick="bla('a')" value="focus the option 'a'">
      </
      form>
      </
      body>
      </
      html
      KOR
      Offshore programming
      -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

      Comment


      • #4
        thanks!!!
        it works now!!!

        Comment

        Working...
        X