Auto-filling one field same as other using JavaScript

You might have noticed that sometimes websites like e-commerce or some government website have two address fields in their forms. One for the primary address and another for the secondary address(or one for billing address and another for the 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.

<!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>
			<input type="submit"
				value="Submit" />
		</form>

		<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>

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

Leave a comment

Your email address will not be published. Required fields are marked *