473,503 Members | 2,197 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

want a clear subroutine that only clears text fields, not my buttons

12 New Member
i want to define a clear button that instead of resetting the entire form, only the text values are set to "". I tried with
document.getElementById("LastNameRow").value = "";
but when my program runs, i enter data into the last name field and then press clear and the data remains. what am i missing? I know that i am entering my clear routine on click because my alerts are printing.
Feb 25 '10 #1
2 1083
Dormilich
8,658 Recognized Expert Moderator Expert
impossible to say without knowing the code.
Feb 26 '10 #2
rbuczynski
10 New Member
Have you checked to ensure that an ID attribute is assigned to the text field element? Sometimes coders confuse NAME and ID attributes or forget one or the other. A simple but common mistake.

Another way to clear text field data with the click of a button is to write a function that loops through all fields in a given form, tests them for field type and clears their values accordingly.

Here's a sample:

Expand|Select|Wrap|Line Numbers
  1. function clearTextFields(oForm) {
  2.   for(var i=0;i<oForm.elements.length;i++) {
  3.     if(oForm.elements[i].type=='text')
  4.       oForm.elements[i].value='';
  5.   }
  6. }
Bind this function to the onClick event of your buttons.

A complete working demonstration using inline event handling:

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function clearTextFields(oForm) {
  3.   for(var i=0;i<oForm.elements.length;i++) {
  4.     if(oForm.elements[i].type=='text')
  5.       oForm.elements[i].value='';
  6.   }
  7. }
  8. </script>
  9. <form>
  10.   <input type="text" id="field1" value="test" />
  11.   <input type="text" id="field2" />
  12.   <input type="button" value="Clear" onClick="clearTextFields(this.form);">
  13. </form>
If you use inline event handling then you can pass a reference to the form easily, as long as the button is an input element and contained within the target form.

Otherwise, if you bind using the traditional or advanced models then you'll have to trace the event to find the target form or else hard-code a fixed reference to a form.
Mar 10 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
7613
by: monika | last post by:
Hi all... I am able to select a student's story and open it in a word doc. the teacher then corrects the story, highlight in colors the comments, strikethrough. In fact all the privileges I...
3
2563
by: Joe | last post by:
I'm wondering how to loop through controls in VB.NET. I have the code from VB6 ok, but I can't figure out how to do it correctly in .NET. This is an example from my VB6 code that loops through...
3
11668
by: Jake | last post by:
How can I have an image that when clicked would clear my textarea field? The textarea field is the only element in my form and has a default value - a couple lines of text.. I tried using a...
5
21020
by: nx-2000 | last post by:
I've got a very large C# forms app and now that its being used in bigger environments we're getting a steady stream of "why does it do this?" problems. The most nagging of which right now is that...
2
1618
by: Paul | last post by:
Hi, I hope somebody can help me with the following. (I have tried searching for a solution on the newsgroups but nothing is quite what I need. Feel free to point me in the direction of anything...
21
5170
by: StriderBob | last post by:
Situation : FormX is mdi child form containing 2 ListViews ListView1 contains a list of table names and 4 sub items with data about each table. ListView2 contains a list of the columns on each...
2
6260
by: Mike | last post by:
Ok I'm having a problem with a listview. I have a listview on a WinForm called ProgramInformationFrm. I call this from from 2 other forms. One is ProgramSearch Frm, the other is OpenNCFrm. The...
4
1696
by: inadsad | last post by:
Good day Group, I have a form with a datagrid and two buttons. On show button datagrid is populated with a datatable and on clear button datagrid is cleared. DataTable.Clear not only removes...
5
10793
aas4mis
by: aas4mis | last post by:
I haven't had much luck with specific controls, their properties and loops in the past. I thought I would share this tidbit of code, feel free to modify/modularize in any way to suit your needs. This...
8
25619
by: charithstudy | last post by:
hi, I have login page , userid and password , when i entered values into them and click browser refresh in MOZILLA , password field clears but userid field text doesnt clears, how to clear...
0
7205
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
7093
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
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7008
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
3177
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...
0
3168
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
399
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...

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.