PDA

View Full Version : getElementbyID with multiple fields



cmaclennan
06-05-2009, 05:46 PM
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>

vwphillips
06-06-2009, 09:46 AM
<!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>