473,321 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

reset select-tag only

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
6 12968
> "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
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
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
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
Thats what I needed, thanks!

Kevin

Sep 29 '05 #6
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Shabam | last post by:
I have a text field that's pre-filled with data. Suppose the user edits it, but decides he wants to reset the data back to the original pre-filled data, how can I do that?
0
by: Mike Mahon | last post by:
The LDAP is located on the mainframe. I can successfully select records using some ASP (Classic) script. I choose to use ASP classic because there are very few examples I could find of using the...
3
by: rmunson8 | last post by:
I have added Panel control to a web page which contains a GridView. The Panel's ScrollBars property is set to auto. When I select the Select CommanField button for a row (which obviously causes a...
3
by: Jim Archer | last post by:
Hi All... I'm been fighting this problem for a few days now, and it seems like it should be simple. But the solution has eluded me so far... I need to flag a record when it is updated or when...
1
by: eyoung | last post by:
I have a list/menu with the first option is "Select Group" followed by all of our groups. The following is a line in a function that should reset this form element to "Select Group"...but it does...
0
by: James Arnold | last post by:
Is it possible to reset all the GridItems within a PropertyGrid to be reset, without recursively looping through each item? I know of PropertyGrid.ResetSelectedProperty, but doesn't this mean I'd...
4
by: desilgalis | last post by:
I am looking for some help to reset a drop down list to its original option value each time a selection is made. My drop down is as follows: <p><font face="Trebuchet MS"><b><font...
2
by: desilgalis | last post by:
I am looking for some help to reset a drop down list to its original option value each time a selection is made. My drop down is as follows: <p><font face="Trebuchet MS"><b><font...
0
by: cmgarnett | last post by:
I am trying to reset an IDENTITY RowNumber back to 1 in a query when the Person's Name is the same. The following query can be used agains the pubs database in SQL Server. I am trying to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.