How to Auto Fill form data on click checkbox using javascript?

AutoFill Permanent address in temp address

File name : index.php

<h1>AutoFill Permanent address in temp address</h1>
<form>
<fieldset>
<legend><b>Permanent 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>





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here