473,782 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ele ments.length-1; i++)
{ var obj = InputForm.eleme nts[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 3890
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.ele ments.length-1; i++)
{ var obj = InputForm.eleme nts[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********@hotm ail.com (Matt) wrote in message news:<ba******* *************** ****@posting.go ogle.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.ele ments.length-1; i++)
{ var obj = InputForm.eleme nts[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,oOb j,sRes,sType
iCount=document .forms(0).lengt h
for (iIdx=0;iIdx<iC ount;iIdx++)
{
oObj=document.f orms(0).item(iI dx)
sType=oObj.type .substring(0,5)
// alert("stype=" + oObj.type + " val=" + oObj.value)
if (sType=="selec" )
{
oObj.selectedIn dex=0
}
else
if (sType=="check" )
oObj.checked=fa lse
else
if (sType=="text")
oObj.value=""
else
if (sType=="radio" )
oObj.checked=tr ue
}
}
Jul 23 '05 #3
jr********@hotm ail.com (Matt) wrote in message news:<ba******* *************** ****@posting.go ogle.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.ele ments.length-1; i++)
{ var obj = InputForm.eleme nts[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,oOb j,sRes,sType
iCount=document .forms(0).lengt h
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<iC ount;iIdx++)
{
oObj=document.f orms(0).item(iI dx)
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.fo rms)&&
((elsRef = document.forms[0]))&&
((elsRef = elsRef.elements ))){

for(var c = elsRef.length;c--;){
oObj = elsRef[c];
sType= (new String(oObj.typ e)).substring(0 ,5);
if(sType=="sele c"){
...
... //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.selectedIn dex=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=fa lse
else
if (sType=="text")
oObj.value=""
else
if (sType=="radio" )
oObj.checked=tr ue
}
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
5078
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 they press "Cancel" they call another ASPX function. My code now is: System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf) System.Web.HttpContext.Current.Response.Write("if (confirm('Are you sure you want to...
1
1623
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 form that is continually displayed. Each time the button is clicked i want the value of a checkbox to be checked and then i want the checkbox to be cleared when the page is reloaded. The problem is that i thought i would clear the checkbox in the...
2
3874
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 interogating the sender)? -- mark
15
5366
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 path of the uploaded image, and resize it with the provided dimensions. My function is below. The current function is returning an error when run from the upload function: A generic error occurred in GDI+. Not sure what exactly that means. From what...
3
2679
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 control. In its inactive state, only a single button appears. If the user clicks on this button, the control becomes active( the rest of the control's functionality becomes visible), and all other instances of this user control on the page should...
4
2224
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 the old code allowed me to do what basically was the following assignment: class SomeClass { private Queue q; SomeClass(Queue q)
4
1464
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 - or better yet how to eliminate - the need to cast from an object when I have specifically filled the combo box with a List(of T) . To replicate this query, just create a new VB Windows application, put a button, a combo box and a text box on the...
5
5802
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 pressed it removes the check but does not re-hide the submit <div> bit. The way i see it, I have 3 options: 1) To simply remove the clear for button. 2) To exclude the said checkbox from the CLEAR button function. 3) To have the CLEAR button run...
53
8418
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 language="javascript" type="text/javascript">
0
9639
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
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10076
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
8964
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7486
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
6729
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
5375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.