Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Clear form

  1. #1
    New Coder
    Join Date
    Oct 2002
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Clear form

    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

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    *keep it simple (TM)

  • #3
    New Coder
    Join Date
    Oct 2002
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I understand your theroy, but am ignorant concerning the code to make it work. Can you jump start me?

    David

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well, do you know what a loop is? do you know how to code one?
    *keep it simple (TM)

  • #5
    New Coder
    Join Date
    Oct 2002
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    New Coder
    Join Date
    Aug 2004
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    Checkout this code....
    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.

  • #7
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by davidwdf
    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:

    1. declare a function with a single parameter which will cover a string of fields you want to reset
    2. declare a loop of the page's form object's elements array
    3. 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.
    4. if this string contains the field name, then set that field's value to blank - basically, an empty string delimited by two double quotes
    5. the loop will repeat for all items in the array of elements in the form until the last field is covered
    *keep it simple (TM)

  • #8
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    damn it, you went and beat me there, afru. was trying to get him to work it for himself.
    *keep it simple (TM)

  • #9
    New Coder
    Join Date
    Oct 2002
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #10
    New Coder
    Join Date
    Aug 2004
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #11
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by afru
    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.


    Quote Originally Posted by afru
    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.)
    *keep it simple (TM)

  • #12
    New Coder
    Join Date
    Oct 2002
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #13
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jbot
    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*
    Last edited by codegoboom; 03-24-2005 at 05:17 PM. Reason: conspiracy theory
    *this message will self destruct in n-seconds*

  • #14
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    david: this is based on my earlier pseudo-code:

    PHP Code:
    function clearFields(oFrm,sFlds)
    {
        for (var 
    i=0aElms=oFrm.elementsaElms.lengthi++)
        {
            if (/
    sFlds/i.test(aElms[i].name)) aElms[i].value="";
        }


    PHP Code:
    <input type="button" onclick="clearFields(this.form, 'field_name_1, field_name_2');" /> 
    hope that helps
    *keep it simple (TM)

  • #15
    New Coder
    Join Date
    Aug 2004
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •