JavaScript | Auto-filling one field same as other

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">    <head>        <meta charset="UTF-8" />        <title>Form Auto Fill</title>        <style>            fieldset {                margin-bottom: 5%;            }        </style>    </head>      <body>        <h1>AutoFill Form</h1>        <form>            //Fields for primary address            <fieldset>                <legend><b>Primary Address</b>              </legend>                <label for="primaryaddress">                  Address:</label>                <input type="text"                        name="Address"                        id="primaryaddress"                        required /><br />                <label for="primaryzip">Zip code:</label>                <input type="text"                        name="Zip code"                        id="primaryzip"                        pattern="[0-9]{6}"                        required /><br />            </fieldset>              <input type="checkbox"                    id="same"                    name="same"                    onchange="addressFunction()" />            <label for="same">              If same secondary address select this box.          </label>              // Fields for secondary address            <fieldset>                <legend><b>Secondary Address</b></legend>                <label for="secondaryaddress">                  Address:              </label>                <input type="text"                        name="Address"                        id="secondaryaddress"                        required /><br />                <label for="secondaryzip">                  Zip code:</label>                <input type="text"                        name="Zip code"                        id="secondaryzip"                       pattern="[0-9]{6}"                        required /><br />            </fieldset>              // Submit button in the form            <input type="submit"                   value="Submit" />        </form>          // JavaScript Code        <script>            function addressFunction() {                if (document.getElementById(                  "same").checked) {                    document.getElementById(                      "secondaryaddress").value =                     document.getElementById(                      "primaryaddress").value;                                        document.getElementById(                      "secondaryzip").value =                     document.getElementById(                      "primaryzip").value;                } else {                    document.getElementById(                      "secondaryaddress").value = "";                    document.getElementById(                      "secondaryzip").value = "";                }            }        </script>    </body></html>
#This is how the form will look like before checking the box:
#And this is how it will look 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.
Explanation: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).
javascript-form
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Hide or show elements in HTML using display property
How to append HTML code to a div using JavaScript ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?