473,383 Members | 1,742 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,383 software developers and data experts.

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.elements.length;
for (var i=0; i<len; i++)
{
if (document.form.elements[i].type == 'radio')
{
var radioGrp = document.form.elements[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 3905
Tyrone Slothrop wrote:
var radioGrp = document.form.elements[i].name;


var radioGrp = document.form.elements[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.com> wrote:
Tyrone Slothrop wrote:
var radioGrp = document.form.elements[i].name;


var radioGrp = document.form.elements[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.elements[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.elements.length;
for (var i=0; i<len; i++)
{
if (document.form.elements[i].type == 'radio')
{
var radioGrp = document.form.elements[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.elements[i].name;
var radioGrp = document.form.elements[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.elements.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.elements[i].name;
rG = document.form.elements[rgName];
rgLength = document.form.elements[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.elements.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.elements[i].name;
rG = document.form.elements[rgName];
rgLength = document.form.elements[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.length;
for ( i=0; i<j; i++) {
el = els[i];

// do text, hidden & textareas
if ( 'text' == el.type
|| 'hidden' == el.type
|| 'textarea' == el.nodeName.toLowerCase() ) {
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
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...
2
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"... ...
0
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...
1
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...
1
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...
2
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...
1
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...
1
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,...
1
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....
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...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.