...

View Full Version : Clear form



davidwdf
03-24-2005, 04:25 PM
My form has 10 fields. I want to click a button(just like Reset) and clear 7 fields and leave 3 alone. How is this done?

thanks, you guys(gals) have ALWAYS come through for me
David

jbot
03-24-2005, 04:55 PM
loop thru the form elements, and change their values respectively. put the names of the fields you want to reset into a string, and compare the name of each field with whatever if is in the string, and if so, then reset the field accordingly.

davidwdf
03-24-2005, 04:58 PM
I understand your theroy, but am ignorant concerning the code to make it work. Can you jump start me?

David

jbot
03-24-2005, 05:07 PM
well, do you know what a loop is? do you know how to code one?

davidwdf
03-24-2005, 05:11 PM
yea, know what a loop is, but thought I could get help on actually writing the code. If it's to big of a pain, just forget it and I'll try some other stuff.

Thanks a bunch
David

afru
03-24-2005, 05:21 PM
Hi,

Checkout this code....


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT LANGUAGE="JavaScript">
<!--
function doReset(){
var formobj=document.getElementById("myform");
var elements=formobj.elements;
for(var i=0; i<7; i++)
elements[i].value='';
}

function clearAnother(){
var textobj=document.getElementById("mytext");
textobj.value='';
}

function fillAgain(){
var formobj=document.getElementById("myform");
var elements=formobj.elements;
for(var i=0; i<elements.length; i++)
if(elements[i].type=="text")
elements[i].value='Have fun';
}


//-->
</SCRIPT>
</HEAD>

<BODY BGCOLOR="#FFFFFF">
<form id="myform">
<input type="text" value="Have fun"><br>
<input type="text" value="Have fun"><br>
<input type="text" value="Have fun"><br>
<input type="text" value="Have fun"><br>
<input type="text" value="Have fun"><br>
<input type="text" value="Have fun"><br>
<input type="text" value="Have fun"><br>
<input type="text" value="Have fun"><br>
<input type="text" value="Have fun" id="mytext"><br>
<input type="text" value="Have fun"><br>
<input type="button" value="reset" onclick="doReset()">
<input type="button" value="clear one more" onclick="clearAnother()">
<input type="button" value="fill again" onclick="fillAgain()">
</form>
</BODY>
</HTML>



Have fun :)

Afru.

jbot
03-24-2005, 05:22 PM
thought I could get help on actually writing the code

that's what I'm doing, telling you what you need to find out. all the relevant keywords are in my post.

anyway, follow this process:


declare a function with a single parameter which will cover a string of fields you want to reset
declare a loop of the page's form object's elements array
with each index in the loop, compare the name of the field with the text of the string of field names you want to reset. use a regular expression for this.
if this string contains the field name, then set that field's value to blank - basically, an empty string delimited by two double quotes
the loop will repeat for all items in the array of elements in the form until the last field is covered

jbot
03-24-2005, 05:24 PM
damn it, you went and beat me there, afru. was trying to get him to work it for himself.

davidwdf
03-24-2005, 05:27 PM
OK guys, I tried working it out before I came to the forum and ended up with a major headache. AFRU gave me what I was after.

But thanks JBOT, you helped also.

David

afru
03-24-2005, 05:29 PM
Oh sorry,

I thought I was helping him out...

I think I have a great road ahead to learn how to behave on forums :)

Afru.

jbot
03-24-2005, 05:33 PM
I thought I was helping him out.

you did. i just think it is better to show the methodology first, then deal with the code later. you learn better that way, because you see how to work out the stages involved and don't just become accustomed to copying-and-pasting.



I think I have a great road ahead to learn how to behave on forums

nah ... you're doing ok as it is. (just a tad over-zealous sometimes LOL.) :)

davidwdf
03-24-2005, 06:01 PM
Hey, afu I want to specify specific fields to clear.

This works great: "function clearAnother(){
var textobj=document.getElementById("mytext");
textobj.value='';

But how do I add more id's("mytext")?

You guys ahve saved me on this one,
David

codegoboom
03-24-2005, 06:09 PM
i just think it is better to show the methodology first, then deal with the code later.

Yeah, it takes at least twice as much work to get people to take a hint, I've noticed. ;)

Then again, most of my questions never get answered here... I just end up rephrasing them, post after post, and accomplish nothing more than getting people to comprehend what the hell I was asking! (maybe a hint...)


*gives up trying on both counts* :rolleyes:

jbot
03-24-2005, 06:15 PM
david: this is based on my earlier pseudo-code:


function clearFields(oFrm,sFlds)
{
for (var i=0, aElms=oFrm.elements; i < aElms.length; i++)
{
if (/sFlds/i.test(aElms[i].name)) aElms[i].value="";
}
}



<input type="button" onclick="clearFields(this.form, 'field_name_1, field_name_2');" />

hope that helps :D

afru
03-24-2005, 06:24 PM
Hi Jbot,

Your idea of sending names as parameters is cool... It didnt striked me... :)
I think it is more than enough for him.

Hey david, you better spend atleast some minutes on JS rather than copy-paste as jbot has advised earlier.

Afru.

davidwdf
03-24-2005, 06:29 PM
Thanks for ALL the advice. I'm weak in JS and have a real problem with time constraints on my job. If someone takes the time to jump start me, I can usually figure it out from there.

Thanks again to all
David

afru
03-24-2005, 06:31 PM
No problem,

Always welcome....

Have a nice day :)

Afru.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum