473,844 Members | 2,241 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Clearing Field Values

I am coding a search form which carries the values to an identical
form on the search results page. A "Clear Fields" button to remove
the values from the previous search is required. Ideally I would
prefer a generic script to do this. I had no problems with the script
until I had to deal with the radio elements.

Following is the problem part of the code:

function resetFields()
{
var len = document.form.e lements.length;
for (var i=0; i<len; i++)
{
if (document.form. elements[i].type == 'radio')
{
var radioGrp = document.form.e lements[i].name;
radioGrp[0].checked = true;
}
}
}

I understand each radio button is an element and assumed that, once
the name is defined, each time the radio element of the same name is
encountered in the script it would be treated as an array
nevertheless. It seems that this is a false assumption since the
radio is not reseting to the "0" value of the group.

What am I doing (or assuming) wrong?

TIA!

Jul 23 '05 #1
6 3945
Tyrone Slothrop wrote:
var radioGrp = document.form.e lements[i].name;


var radioGrp = document.form.e lements[i];

But why not use a plain <input type="reset" />?
JW

Jul 23 '05 #2
On Sat, 25 Jun 2005 22:55:18 +0200, "Janwillem Borleffs"
<jw@jwscripts.c om> wrote:
Tyrone Slothrop wrote:
var radioGrp = document.form.e lements[i].name;


var radioGrp = document.form.e lements[i];

But why not use a plain <input type="reset" />?
JW


Because it doesn't work when you carry the variables from the current
search on the results page. Reset will work only if you start with a
clean slate.

Anyway:

var radioGrp = document.form.e lements[i];
radioGrp[0].checked = 1;

returns an error: radioGrp[0] has no properties.

Maybe this isn't possible???
Jul 23 '05 #3
Tyrone Slothrop wrote:
I am coding a search form which carries the values to an identical
form on the search results page. A "Clear Fields" button to remove
the values from the previous search is required. Ideally I would
prefer a generic script to do this. I had no problems with the script
until I had to deal with the radio elements.

Following is the problem part of the code:

function resetFields()
{
var len = document.form.e lements.length;
for (var i=0; i<len; i++)
{
if (document.form. elements[i].type == 'radio')
{
var radioGrp = document.form.e lements[i].name;
this will set 'radioGrp' to the name of element[i] (if it has one);
radioGrp[0].checked = true;
This may work in IE (I can't test it right now) but you are depending
on the name of an element in the radio group being treated as a
global variable and being a reference to a collection radio buttons.

In any case, the way to get the radio group is to use the form and
elements collection with the name that you just got:

var radioGrpName = document.form.e lements[i].name;
var radioGrp = document.form.e lements[radioGrpName];
radioGrp[0].checked = true;

though it could be done with fewer intermediate steps.
}
}
}

I understand each radio button is an element and assumed that, once
the name is defined, each time the radio element of the same name is
encountered in the script it would be treated as an array
nevertheless.
Once you fix your script as above, you will find that it is treated
as an HTML element collection, which is like an array but different.
It seems that this is a false assumption since the
radio is not reseting to the "0" value of the group.


Your assumption was correct, you were just addressing the collection
incorrectly.

Once you have reset to the first button, you probably want to skip to
the end of the radio group, otherwise your loop will reset the group
once for each element (not a big deal but a bit of a CPU-cycle
waster).

[...]

--
Rob
Jul 23 '05 #4
> Snipping all previous posts....

This works, though I would think there is a better way, though nothing
else I tried (nor anything suggested) worked:

function resetFields()
{
var k = 1;
var rgName, rgLength, rG;
var len = document.form.e lements.length;
for (var i=0; i<len; i++)
{
// Code removed here to show only radio reset
if (document.form. elements[i].type == 'radio')
{
rgName = document.form.e lements[i].name;
rG = document.form.e lements[rgName];
rgLength = document.form.e lements[rgName].length;
if (k == rgLength) { rG[0].checked = true; }
k++;
}
}
}

Until at least the second iteration of the radio elements, the radio
group is not an array. Trying to reset the first radio of the group
(and ignoring any further elements of the same name) prior to it being
an array failed, ie. rG.checked = true.

This seems damned complex for something that should be simple.

BTW, thanks those of you who offered their assistance!

Jul 23 '05 #5
Tyrone Slothrop wrote:
This works, though I would think there is a better way, though nothing
else I tried (nor anything suggested) worked:


Here's an alternative using partly DOM, with the drawback that each element
of an element group is still accessed and it might not be supported by all
browsers:

var elems = form.childNodes ;
for (var i = 0; i < elems.length; i++) {
if (elems[i].type == 'radio') {
var name = elems[i].getAttribute(' name');
if (form.elements[name].length) {
form.elements[name][0].checked = true;
} else {
form.elements[name].checked = true;
}
}
}
JW

Jul 23 '05 #6
Tyrone Slothrop wrote:
Snipping all previous posts....

This works, though I would think there is a better way, though nothing
else I tried (nor anything suggested) worked:

function resetFields()
{
var k = 1;
var rgName, rgLength, rG;
var len = document.form.e lements.length;
for (var i=0; i<len; i++)
{
// Code removed here to show only radio reset
if (document.form. elements[i].type == 'radio')
{
rgName = document.form.e lements[i].name;
rG = document.form.e lements[rgName];
rgLength = document.form.e lements[rgName].length;
if (k == rgLength) { rG[0].checked = true; }
k++;
}
}
}


There's a simplified version of this bit below. I assume you are
passing a reference to the form to the function, if not, then you need
to assign a value to 'form' using getElementById or the forms
collection.

Until at least the second iteration of the radio elements, the radio
group is not an array.
It's only an array when you make it one.
Trying to reset the first radio of the group
(and ignoring any further elements of the same name) prior to it being
an array failed, ie. rG.checked = true.
It can be done, see below. You need to add the length of the array
less one.

This seems damned complex for something that should be simple.
That's why there's a reset button :-)

You are making it a little harder than it should be:

<script type="text/javascript">
function resetFields(f) {
var rGrp, el, els = f.elements;
var i, j = f.elements.leng th;
for ( i=0; i<j; i++) {
el = els[i];

// do text, hidden & textareas
if ( 'text' == el.type
|| 'hidden' == el.type
|| 'textarea' == el.nodeName.toL owerCase() ) {
el.value = '';

// reset radio groups
} else if ( 'radio' == el.type ) {
rGrp = f.elements[el.name];
rGrp[0].checked = true;
i += rGrp.length-1; // Skip to end of group

// Do other elements

}
}
}
</script>

BTW, thanks those of you who offered their assistance!

--
Rob
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2894
by: Robin | last post by:
Ok, I have a form that on clicking of the Update button first updates the specific record in the db, then Inserts if the vMemo field is not empty. The problem that I'm having is that After updating if you hit the key (refresh) it inserts another record ... I have tried: 1. Clearing the vMemo field after the insert is done (vMemo = "") 2. Setting the value on the form for this field to "" Neither of these are working. It's not...
2
2652
by: Charles M. Fish, Sr. | last post by:
I’m so tired from banging this problem around all day, I hope I can explain it succinctly & accurately. I want to execute a function immediately following a click on <input type="RESET"... The function will insert today’s date into a <input type="TEXT"… field. It does it just fine with <form onload="insertDate();" where inside the ‘insertDate’ fn, I have document.form.dateField.value = dateString. The closest I’ve been able to do...
0
1915
by: Gary | last post by:
I have a problem with the datagrid not clearing when the datasource changes, a few fields remain on the screen and even when new data would fill those grid squares the old data remains. I wind up with 101 Another field
1
4261
by: hortoristic | last post by:
We are using JavaScript to Enable/Disable certain fields on web pages based on business rules. A simple example is if when using an option type tag, and the two options are Yes and No. If YES is selected - enable a field to use the M$ Datepicker. Using the code below works for most of our fields, however the problem is that when the field is re-enabled - it remembers the original date or data prior to it being disabled - despite the...
1
2339
by: Tyros | last post by:
I'm new to C# and I'm dynamically creating a table control that contains a text control inside one of the cells. I load the values of each text field with an SQL call. The form is simple and I'm getting HR information from the DB. When the page loads the first time and looks up the customers information based on his/her NT login ID everything works great. I want to have the ability to change the NT login ID in the form and then repost the...
2
1705
by: John Smith | last post by:
Hi folks, I have a form with ASP.NET web controls. At the end of the form there's the "Clear" button to clear the available values and start over. How do I do that? The following did not work because it unloaded the controls, and not just clearing the values: - this.Controls.Clear();
1
4274
by: scprosportsman | last post by:
Please help guys, i am trying to set up a database here at work and im fairly new to access in terms of writing functions and queries and stuff. I have 2 different places on my design that will require checking a check box to tell which category something will pertain to. Well after each record is entered i want the check to remain with that record and auto clear when going to the next record so i can input something else if i have a...
1
1755
by: Vayse | last post by:
I have a databound form, frmClients. One of the field is a combo box, comDepartment, which lets a user select a department for the Client. But I can't figure out how to clear the box. That is, sometimes a Client is dropped by a Department. How would a user clear the combo box so that no Department is selected? With the combo box dropdown style: Dropdown: User must pick from the list, so there is no way of clearing the box Dropdown List:...
1
1749
by: ray well | last post by:
i'm loading a combobox from a database in code, by setting the the DataSouce to a table, and the DisplayMember to a field. it loads the first row into the text area of the combobox automatically. i don't want that. i used to clear it up by setting ComboBox1.Text="", after loading it. but i had to put the combobox on a tab page of a tab control. ComboBox1.Text="" works only if the tab page has the focus when that code
0
9878
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10697
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10328
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7874
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7050
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5903
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4518
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4107
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.