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.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    copying the values in textboxes

    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.

    Code:
    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,
    Last edited by SunJune; 01-03-2011 at 08:59 PM.

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    You could use [] notation in a loop:

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

  • Users who have thanked Logic Ali for this post:

    SunJune (01-03-2011)

  • #3
    New Coder
    Join Date
    Dec 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Just a quick question, what the expression (df=document.form ) in the loop is doing?

    Thanks

  • #4
    New Coder
    Join Date
    Dec 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Got it thanks!

  • #5
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by SunJune View Post
    Just a quick question, what the expression (df=document.form ) in the loop is doing?

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


  •  

    Posting Permissions

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