469,643 Members | 1,514 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,643 developers. It's quick & easy.

Generic clearForm function when click CLEAR button

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[i];
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!!
Jul 23 '05 #1
5 3669
Matt wrote:
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[i];
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[i];
if(obj.value && obj.type != "submit" && obj.type != "button"){
obj.value="";
}
}
}
You could hit "reset" button to do the same thing.
Mick
Jul 23 '05 #2
jr********@hotmail.com (Matt) wrote in message news:<ba**************************@posting.google. com>...
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[i];
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
}
}
Jul 23 '05 #3
jr********@hotmail.com (Matt) wrote in message news:<ba**************************@posting.google. com>...
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[i];
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.
Jul 23 '05 #4
bruce wrote:
<snip>
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?
}

Jul 23 '05 #5
Matt wrote:
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 :)

--
Matt Kruse
Javascript Toolbox: http://www.mattkruse.com/javascript/
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Joshua Weir | last post: by
2 posts views Thread by mark | last post: by
3 posts views Thread by Phillip N Rounds | last post: by
4 posts views Thread by Adam Clauss | last post: by
53 posts views Thread by souporpower | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.