Results 1 to 2 of 2

Thread: getElementbyID with multiple fields

  1. #1
    Join Date
    Jun 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation getElementbyID with multiple fields

    Hi Guys, hoping someone can help i've been looking around trying to gain some insight into how i can achieve what i want to do as javascript is not my forte.

    Essentially I have a form that has customer info and shipping info and if the shipping info is the same as the customer I would like to have a checkbox to automatically fill in the fields with the values from the customer portion.

    In my searching I found a script that will do this with 1 field but cant seem to and dont know how to make it work with multiple fields. Any help is greatly appreciated.

    Thanks in advance

    Code I found for 1 field
    Field1: <input type="text" id="field1" value="Hello World!">
    <br />
    Field2: <input type="text" id="field2">
    <br /><br />
    Click the button below to copy the content of Field1 to Field2.
    <br />
    <button onClick="document.getElementById('field2').value=
    document.getElementById('field1').value">Copy Text</button>

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script  type="text/javascript">
    /*<![CDATA[*/
    
    function Copy(cb,nme){
     var from=zxcByClassName(nme);
     var to=zxcByClassName(nme+1);
     for (var z0=0;z0<from.length;z0++){
      if (to[z0]) to[z0].value=cb.checked?from[z0].value:'';
     }
    }
    
    function zxcByClassName(nme,el,tag){
     if (typeof(el)=='string') el=document.getElementById(el);
     el=el||document;
     for (var tag=tag||'*',reg=new RegExp('\\b'+nme+'\\b'),els=el.getElementsByTagName(tag),ary=[],z0=0; z0<els.length;z0++){
      if(reg.test(els[z0].className)) ary.push(els[z0]);
     }
     return ary;
    }
    
    
    
    /*]]>*/
    </script></head>
    
    <body>
    <form>
    Field1: <input type="text" class="field" value="Hello World!">
    Field2: <input type="text" class="field" value="Hello World 2">
    <br />
    <br /><br />
    Click the button below to copy the content of Field1 to Field2.
    <br />
    <input type="checkbox" onClick="Copy(this,'field');">Copy Text</button><br />
    Field1: <input type="text" class="field1">
    Field2: <input type="text" class="field1">
    <br />
    <br />
    Field1: <input type="text" class="tom" value="Hello World!">
    Field2: <input type="text" class="tom" value="Hello World 2">
    <br />
    <br /><br />
    Click the button below to copy the content of Field1 to Field2.
    <br />
    <input type="checkbox" onClick="Copy(this,'tom');">Copy Text</button><br />
    Field1: <input type="text" class="tom1">
    Field2: <input type="text" class="tom1">
    </form>
    
    </body>
    
    </html>

Bookmarks

Posting Permissions

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