fill form - greens for good answer

Discussion in 'JavaScript' started by klown, Oct 31, 2006.

  1. #1
    I'm working on a script to do some automatic filling. I've ran into a problem that sometimes I won't know the name of a form so its hard for me to do an autofill. Here is the code to fill text from form1 to form2, how can I do the same if the second form has no name?

    <SCRIPT LANGUAGE=JavaScript>
    <!--
    function fill()
    {
    	document.form2.name.value=document.form1.name.value;
    	document.form2.age.value=document.form1.age.value;
    }
    
    //-->
    </SCRIPT>
    
    <input type="button" onClick="javascript:fill()" value="Fill Forms">
    
    <form method="get" action="" name="form1">
    Name <input name="name" type="text">
    Age <input name="age" type="text">
    </form>
    
    <form method="get" action="" name="form2">
    Name 2 <input name="name" type="text">
    Age 2 <input name="age" type="text">
    </form>
    Code (markup):
     
    klown, Oct 31, 2006 IP
  2. sxg

    sxg Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Wouldn't you be able to access the form by something like:
    document.forms[number].elements[number]

    For example, document.forms[0].elements[0] would refer to first form, first element. This way, you can code without knowing the element name as well.

    Hope this helps.
     
    sxg, Nov 1, 2006 IP