473,405 Members | 2,354 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,405 software developers and data experts.

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 3863
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: cheezebeetle | last post by:
ok, so I am having problems passing in an ASPX function into the Javascript in the codebehind page. I am simply using a confirm call which when they press "OK" they call this ASPX function, when...
1
by: Joshua Weir | last post by:
Hi there, I just found out that when I have a button on an aspx page and i click it, the page_load event function executes before the button click event function. Why is this so? I have a web...
2
by: mark | last post by:
Is there a means of raising an event when ANY button on a windows application form is clicked wherein the actual button clicked can be determined in the generic click events code (eg by way of...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
3
by: Phillip N Rounds | last post by:
I'm writing a user control which has two states: Active & InActive. I additionally am required that there to be only one active control per page, and all logic has to be contained within the...
4
by: Adam Clauss | last post by:
I ran into a problem a while back when attempting to convert existing .NET 1.1 based code to .NET 2.0 using Generic collections rather than Hashtable, ArrayList, etc. I ran into an issue because...
4
by: dmac | last post by:
Below is some really simple code - its just a trivial class used to populate a combo box from which I want to pull out one of the properties of the selected object. I am just curious to know why -...
5
by: plumba | last post by:
Ok, another query.... I have a checkbox at the bottom of my form which when checked unhides a <div> block which displays the submit button. The problem I have is when the clear form button is...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
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...

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.