resetting an HTML combo box

  • Thread starter Richard Haygreen
  • Start date
R

Richard Haygreen

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.
 
M

McKirahan

Richard Haygreen said:
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.

document.{form_name}.{select_name}.selectedIndex = -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.selectedIndex = -1;
}
}
</script>
</head>
<body>
<form name="form1">
<select name="ContactMethod"
onchange="selected(this)">
<option>
<option>Phone
<option>Email
<option>Pager
<option>Other
</select>
</form>
</body>
</html>
 
F

Fred Oz

Richard said:
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.

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.
 
M

Michael Winter

How do I reset the combo box so it displays the first option in the
combo box (which is the default)??
[snip]

document.{form_name}.{select_name}.selectedIndex = -1;

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

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

[snip]

Mike
 
R

Richard Haygreen

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top