Generic clearForm function when click CLEAR button

M

Matt

I want to write a generic clearForm function to clear the form when
the user click CLEAR button.

Here's my attempts, because I want to take care all html controls. I
think I need to test
if the control is submit button, regular button. But I don't know
what I should do on drop down box?

function clearForm()
{ var i=0;
for (i=0; i<InputForm.elements.length-1; i++)
{ var obj = InputForm.elements;
document.write(obj.type); //runtime error: object doesn't support
this property or method
if (obj.type != "submit" && obj.type != "button")
obj.value = "";
}
}

any ideas?? thanks!!
 
M

Mick White

Matt said:
I want to write a generic clearForm function to clear the form when
the user click CLEAR button.

Here's my attempts, because I want to take care all html controls. I
think I need to test
if the control is submit button, regular button. But I don't know
what I should do on drop down box?

function clearForm()
{ var i=0;
for (i=0; i<InputForm.elements.length-1; i++)
{ var obj = InputForm.elements;
document.write(obj.type); //runtime error: object doesn't support
this property or method
if ()
obj.value = "";
}
}

any ideas?? thanks!!



function clearForm(form){
f=form.length
for (i=0; i<f-1; i++){
obj=form;
if(obj.value && obj.type != "submit" && obj.type != "button"){
obj.value="";
}
}
}
You could hit "reset" button to do the same thing.
Mick
 
B

bruce

I want to write a generic clearForm function to clear the form when
the user click CLEAR button.

Here's my attempts, because I want to take care all html controls. I
think I need to test
if the control is submit button, regular button. But I don't know
what I should do on drop down box?

function clearForm()
{ var i=0;
for (i=0; i<InputForm.elements.length-1; i++)
{ var obj = InputForm.elements;
document.write(obj.type); //runtime error: object doesn't support
this property or method
if (obj.type != "submit" && obj.type != "button")
obj.value = "";
}
}

any ideas?? thanks!!




Here's a function I wrote a zillion years ago. I offer it to you with
no guarantees, but maybe it will help you:

function ClearFields ()
{
var iIdx,iCount,oObj,sRes,sType
iCount=document.forms(0).length
for (iIdx=0;iIdx<iCount;iIdx++)
{
oObj=document.forms(0).item(iIdx)
sType=oObj.type.substring(0,5)
// alert("stype=" + oObj.type + " val=" + oObj.value)
if (sType=="selec")
{
oObj.selectedIndex=0
}
else
if (sType=="check")
oObj.checked=false
else
if (sType=="text")
oObj.value=""
else
if (sType=="radio")
oObj.checked=true
}
}
 
B

bruce

I want to write a generic clearForm function to clear the form when
the user click CLEAR button.

Here's my attempts, because I want to take care all html controls. I
think I need to test
if the control is submit button, regular button. But I don't know
what I should do on drop down box?

function clearForm()
{ var i=0;
for (i=0; i<InputForm.elements.length-1; i++)
{ var obj = InputForm.elements;
document.write(obj.type); //runtime error: object doesn't support
this property or method
if (obj.type != "submit" && obj.type != "button")
obj.value = "";
}
}

any ideas?? thanks!!



as far as a select control, I have set the length to zero, thereby
eliminating all the <option> items in the select control.
 
R

Richard Cornford

bruce wrote:
function ClearFields ()
{
var iIdx,iCount,oObj,sRes,sType
iCount=document.forms(0).length

Treating the - document.forms - collection as a function, calling it and
passing an argument, is a Microsoftism and should not be expected to
work on non-IE browsers (though it does on some as they are forced to
put effort in to emulating IE's non-standard behaviour to accommodate
script authors who aren't interested in other browsers).

Normal (cross-browser) references to forms through the -
document.forms - collection would use a bracket notation property
accessor, and work on every browser that understands what a form is,
including IE.

iCount = document.forms[0].length;
for (iIdx=0;iIdx<iCount;iIdx++)
{
oObj=document.forms(0).item(iIdx)

Re-resolving the reference to the form within a loop is inefficient, and
treating the FORM element as implementing a HTMLCollection interface is
relying on a non-specified coincidence that, although it is common in
implementations, should not be relied upon.

The controls within a from are traditionally, and by W3C HTML DOM
specification, available as indexed members of the FORM's - elements -
collection, and can be reliably accessed through that collection on
every browser that understands what a form is:-

var elsRef, oObj, sType;
if((document.forms)&&
((elsRef = document.forms[0]))&&
((elsRef = elsRef.elements))){

for(var c = elsRef.length;c--;){
oObj = elsRef[c];
sType= (new String(oObj.type)).substring(0,5);
if(sType=="selec"){
...
... //etc.

}
}
}

Doing it this way also means that when the function is to be applied in
a different context, to a form with a different index, there is only one
place in the function where the index needs to be updated. Though a more
general function would be passed the from reference as an argument.
sType=oObj.type.substring(0,5)
// alert("stype=" + oObj.type + " val=" + oObj.value)
if (sType=="selec")
{
oObj.selectedIndex=0
}

If a SELECT element is select-multiple then setting its -
selectedIndex - to zero will not necessarily have the desired result.
else
if (sType=="check")
oObj.checked=false
else
if (sType=="text")
oObj.value=""
else
if (sType=="radio")
oObj.checked=true
}

What happened to TEXTAREA elements?
 
M

Matt Kruse

Matt said:
I want to write a generic clearForm function to clear the form when
the user click CLEAR button.

What does "clear" mean, exactly?
In input controls, it's obvious - setting the value to ''

But what about other controls:
- Checkbox: Does it mean unchecking all checkboxes, or checking only
boxes where value='' ?
- Radio: You can't uncheck all radio buttons in a group, so which one
stays selected?
- Select: Do you select the option with value='' ? What if there are
none?
- Multi-select: Do you unselect all options, or select options with
value='' ?
- File: You can't set the value of these at all!

In order to program to requirements, the requirements need to be clear :)
 

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,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top