By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,814 Members | 2,162 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,814 IT Pros & Developers. It's quick & easy.

reset select-tag only

P: n/a
Hi,

is it possible to reset a SELECT-Tag to it's default value but not
reset all other form fields?
Here's an example:

<select id="myselect" name="myselect">
<option> value1 </option>
<option> value2 </option>
<option selected> value3 </option>
<option> value4 </option>
</select>

What I need is a Javascript function which resets the myselect-field to
it's inital value3. It may have been changed by the user to let's say
value4.
Setting the selectedIndex=0 will not work as it will select the first
(value1) option instead of value3. Same thing with selectedIndex=-1
which will select nothing.

Thanks,

Kevin.

Sep 29 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
> "go****@kevinkempfer.de" <go****@kevinkempfer.de> wrote:
news:11**********************@o13g2000cwo.googlegr oups.com....

Hi,

is it possible to reset a SELECT-Tag to it's default value but not
reset all other form fields?
Here's an example:

<select id="myselect" name="myselect">
<option> value1 </option>
<option> value2 </option>
<option selected> value3 </option>
<option> value4 </option>
</select>

What I need is a Javascript function which resets the myselect-field
to it's inital value3. It may have been changed by the user to let's
say value4.
Setting the selectedIndex=0 will not work as it will select the first
(value1) option instead of value3. Same thing with selectedIndex=-1
which will select nothing.

<select id="myselect" name="myselect" onchange="this.selectedIndex='2'">
<option> value1 </option>
<option> value2 </option>
<option selected> value3 </option>
<option> value4 </option>
</select>

--
BootNic Thursday, September 29, 2005 12:31 PM

You can discover what your enemy fears most by observing the means he uses to frighten you.
*Eric Hoffer*

Sep 29 '05 #2

P: n/a
Thanks, but I don't know which option will be selected by default.
Maybe next time it's value2. So I really need some reset-function.

Kevin

Sep 29 '05 #3

P: n/a
go****@kevinkempfer.de wrote:
is it possible to reset a SELECT-Tag to it's default value but not
reset all other form fields?


Yes. You need to loop through all the options, and check if each has
defaultSelected==true. If it does, select it.

Or, using my functions from http://www.mattkruse.com/javascript/validations/
you could just do:

var sel = document.forms["myform"].mySelectElement;
setInputValue(sel,getInputDefaultValue(sel));

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Sep 29 '05 #4

P: n/a
go****@kevinkempfer.de wrote:
Thanks, but I don't know which option will be selected by default.
Maybe next time it's value2. So I really need some reset-function.


That doesn't make any sense.
You don't need to know which option is selected by default. Your code should
just do something like

for (var i=0;i<sel.options.length;i++) {
if (sel.options[i].defaultSelected) {
sel.options[i].selected = true;
}
else {
sel.options[i].selected = false;
}
}

(written that way so it will work on multiple selects also).

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Sep 29 '05 #5

P: n/a
Thats what I needed, thanks!

Kevin

Sep 29 '05 #6

P: n/a
Matt Kruse wrote:
for (var i=0;i<sel.options.length;i++) {
if (sel.options[i].defaultSelected) {
sel.options[i].selected = true;
}
else {
sel.options[i].selected = false;
}
}


And actually, this would be more compact, but maybe less readable to someone
learning:

for (var i=0;i<sel.options.length;i++) {
sel.options[i].selected = sel.options[i].defaultSelected;
}
--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Sep 29 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.