...

View Full Version : copying the values in textboxes



SunJune
01-03-2011, 09:55 PM
Hi everyone,

Is there a shorter and cleaner way of performing the following:

The objective is to get the value/input from the first set of textboxes in the form and copy them to other set of textboxes.

Outline of the form is as follows:

First set of textboxes
Company
Address
City
State
Zip
Country

Select number of users -->1 2 3

User 1
Company
Address
City
State
Zip
Country

User 2
Company
Div
Address1
Address2
City
State
Zip
Country

User 3
Company
Address
City
State
Zip
Country

=============================================
At the moment the code looks like this where (comp,div, addra,addrb,city,state,zip and country) are the name attributes of the first set of text boxes and (comp1.....country1,comp2.....country2, comp3....country3) are the name attributes for the textboxes for user 1,2 and 3 respectively.



function compAddress(){

document.form.comp1.value = document.form.comp.value;
document.form.div1.value = document.form.div.value;
document.form.addra1.value = document.form.addra.value;
document.form.addrb1.value = document.form.addrb.value;
document.form.city1.value = document.form.city.value;
document.form.state1.value = document.form.state.value;
document.form.zip1.value = document.form.zip.value;
document.form.country1.value = document.form.country.value;

document.form.comp2.value = document.form.comp.value;
document.form.div2.value = document.form.div.value;
document.form.addra2.value = document.form.addra.value;
document.form.addrb2.value = document.form.addrb.value;
document.form.city2.value = document.form.city.value;
document.form.state2.value = document.form.state.value;
document.form.zip2.value = document.form.zip.value;
document.form.country2.value = document.form.country.value;

document.form.comp3.value = document.form.comp.value;
document.form.div3.value = document.form.div.value;
document.form.addra3.value = document.form.addra.value;
document.form.addrb3.value = document.form.addrb.value;
document.form.city3.value = document.form.city.value;
document.form.state3.value = document.form.state.value;
document.form.zip3.value = document.form.zip.value;
document.form.country3.value = document.form.country.value;


Thanks,

Logic Ali
01-03-2011, 10:03 PM
You could use [] notation in a loop:


for( var i = 1, df = document.form; i< 4; i++ )
{
df[ 'comp' + i ] = df.comp.value;
.........
}

SunJune
01-03-2011, 10:28 PM
Just a quick question, what the expression (df=document.form ) in the loop is doing?

Thanks

SunJune
01-03-2011, 10:40 PM
Got it thanks!

Logic Ali
01-03-2011, 10:41 PM
Just a quick question, what the expression (df=document.form ) in the loop is doing?

ThanksIt creates a direct reference to document.form, which is faster to resolve and saves time in a loop.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum