resetting an HTML combo box

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

    resetting an HTML combo box

    I have an HTML form which includes a combo box with 4 options, which is used
    to select a method of contact (phone, email etc...). If the user selects an
    invalid option (for example, they have selected phone, but have not supplied
    a phone number) an alert box pops up. How do I reset the combo box so it
    displays the first option in the combo box (which is the default)??

    Many thanks in advance.


  • McKirahan

    #2
    Re: resetting an HTML combo box

    "Richard Haygreen" <rjth3@kent.ac. uk> wrote in message
    news:cjjq8q$p6u $1@athena.ukc.a c.uk...[color=blue]
    > I have an HTML form which includes a combo box with 4 options, which is[/color]
    used[color=blue]
    > to select a method of contact (phone, email etc...). If the user selects[/color]
    an[color=blue]
    > invalid option (for example, they have selected phone, but have not[/color]
    supplied[color=blue]
    > a phone number) an alert box pops up. How do I reset the combo box so it
    > displays the first option in the combo box (which is the default)??
    >
    > Many thanks in advance.
    >[/color]

    document.{form_ name}.{select_n ame}.selectedIn dex = -1;

    For example, watch for word-wrap.

    <html>
    <head>
    <title>selreset .htm</title>
    <script type="text/javascript">
    function selected(that) {
    var form1 = document.form1;
    // substitute your validation below.
    if (1==1) {
    alert("Invalid selection!");
    that.selectedIn dex = -1;
    }
    }
    </script>
    </head>
    <body>
    <form name="form1">
    <select name="ContactMe thod"
    onchange="selec ted(this)">
    <option>
    <option>Phone
    <option>Email
    <option>Pager
    <option>Other
    </select>
    </form>
    </body>
    </html>


    Comment

    • Michael Winter

      #3
      Re: resetting an HTML combo box

      On Fri, 01 Oct 2004 15:01:30 GMT, McKirahan <News@McKirahan .com> wrote:
      [color=blue]
      > "Richard Haygreen" <rjth3@kent.ac. uk> wrote in message
      > news:cjjq8q$p6u $1@athena.ukc.a c.uk...
      >[color=green]
      >> How do I reset the combo box so it displays the first option in the
      >> combo box (which is the default)??[/color][/color]

      [snip]
      [color=blue]
      > document.{form_ name}.{select_n ame}.selectedIn dex = -1;[/color]

      The OP asked for the first option. That removes any selection.

      document.forms['formName'].elements['elementName'].selectedIndex = 0;

      [snip]

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • Fred Oz

        #4
        Re: resetting an HTML combo box

        Richard Haygreen wrote:[color=blue]
        > I have an HTML form which includes a combo box with 4 options, which is used
        > to select a method of contact (phone, email etc...). If the user selects an
        > invalid option (for example, they have selected phone, but have not supplied
        > a phone number) an alert box pops up. How do I reset the combo box so it
        > displays the first option in the combo box (which is the default)??
        >
        > Many thanks in advance.
        >
        >[/color]

        I would think carefully about re-setting it. If the user's input fails
        validation, you are going to clear their selection and (presumably)
        whatever they typed in. So as punishment for maybe one bad keystroke,
        they now have to re-select (say) e-mail and type in maybe 15 characters
        all over again.

        Maybe what you are doing is fine, but just think about it from your
        users' perspective.


        Cheers, Fred.

        Comment

        • Richard Haygreen

          #5
          Re: resetting an HTML combo box

          > I would think carefully about re-setting it. If the user's input fails[color=blue]
          > validation, you are going to clear their selection and (presumably)
          > whatever they typed in. So as punishment for maybe one bad keystroke,
          > they now have to re-select (say) e-mail and type in maybe 15 characters
          > all over again.
          >
          > Maybe what you are doing is fine, but just think about it from your users'
          > perspective.
          >
          >
          > Cheers, Fred.[/color]

          Yes, I would agree with you, my intention is not to punish the user, but to
          force them to make a selection. If they select phone, and have not entered a
          phone number I want to reset the combo box; leaving them two choices: select
          a different option or enter a phone number. At the moment the page tells the
          user they have errored, but does nothing about it. I am trying to build in a
          fail safe.

          Many thanks for all your help.


          Comment

          Working...