Skip to content
Related Articles

Related Articles

JavaScript | Auto-filling one field same as other

View Discussion
Improve Article
Save Article
  • Last Updated : 18 Nov, 2020

(This article takes some prior knowledge of HTML, CSS, and JavaScript.)
You might have noticed that sometimes websites like e-commerce or some government website have two address fields in their forms. One for primary address and another for secondary address(or one for billing address and another for shipping address etc).
Most of the time people have the same primary and secondary addresses and to save us from the tedious work of re-entering the same data again they have some kind of option to automatically copy the contents of one field into another.
We are going to see how to make such kind of Auto-Complete form using JavaScript.
In the form we are going to discuss, there is a checkbox and whenever it is checked, the code automatically copies values from the primary address and primary zip-code to the secondary address and secondary zip-code respectively. If the checkbox is unchecked, these fields will go blank.
Here is the simple code for such kind of form:

<!DOCTYPE html>
<html lang="en">
        <meta charset="UTF-8" />
        <title>Form Auto Fill</title>
            fieldset {
                margin-bottom: 5%;
        <h1>AutoFill Form</h1>
            //Fields for primary address
                <legend><b>Primary Address</b>
                <label for="primaryaddress">
                <input type="text" 
                       required /><br />
                <label for="primaryzip">Zip code:</label>
                <input type="text" 
                       name="Zip code" 
                       required /><br />
            <input type="checkbox" 
                   onchange="addressFunction()" />
            <label for="same">
              If same secondary address select this box.
            // Fields for secondary address
                <legend><b>Secondary Address</b></legend>
                <label for="secondaryaddress">
                <input type="text" 
                       required /><br />
                <label for="secondaryzip">
                  Zip code:</label>
                <input type="text" 
                       name="Zip code" 
                       required /><br />
            // Submit button in the form
            <input type="submit"
                   value="Submit" />
        // JavaScript Code
            function addressFunction() {
                if (document.getElementById(
                  "same").checked) {
                      "secondaryaddress").value = 
                      "secondaryzip").value = 
                } else {
                      "secondaryaddress").value = "";
                      "secondaryzip").value = "";

#This is how the form will look like before checking the box:
before checking box

#And this is how it will look after checking the box:
after checking the box
Note: features like

  • ‘required'(line 18, 20, 29, 31)-ensures that form will only be submitted if these fields are non-empty;
  • ‘pattern = “[0-9]{6}”‘(line 20, 31)-ensures that format of zip-code is correct i.e., six digit zip-code.

When the checked state of the checkbox is changed the ‘onchange'(see line 23) event will occur which will call the ‘addressFunction()’.
If the box is checked, values of primary address and primary zip-code will be copied to secondary address and secondary zip-code(by using ‘getElementById()’ function we are referring to an element of particular Id and ‘.value’ to access the value at that particular Id element).
Otherwise, these fields will remain blank so that it can be filled by the user(in case of different primary and secondary addresses).

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!