Reputation: 699
I need a javascript function for copying the value of one input field to another input field based on checkbox selection. However i did some javascript code on click,
<script>
function copyTextValue() {
var text1 = document.getElementById("Name1").value;
document.getElementById("Name2").value = text1;
document.getElementById("Name3").value=text1;
}
</script>
<input type="checkbox" name="check1" onclick="copyTextValue();"/>
Now i need to delete the copied values in those two boxes on uncheck. I stuck up with this. Any help?
Upvotes: 5
Views: 43801
Reputation: 1302
By using upper ans I copied Billing address into shipping address in woocommerce
function copyTextValue(bf) {
var billing_first_name = bf.checked ? document.getElementById("billing_first_name").value : '';
var billing_last_name = bf.checked ? document.getElementById("billing_last_name").value : '';
var billing_address_1 = bf.checked ? document.getElementById("billing_address_1").value : '';
var billing_address_2 = bf.checked ? document.getElementById("billing_address_2").value : '';
var billing_city = bf.checked ? document.getElementById("billing_city").value : '';
var billing_state = bf.checked ? document.getElementById("billing_state").value : '';
var billing_postcode = bf.checked ? document.getElementById("billing_postcode").value : '';
document.getElementById("shipping_first_name").value = billing_first_name;
document.getElementById("shipping_last_name").value = billing_last_name;
document.getElementById("shipping_address_1").value = billing_address_1;
document.getElementById("shipping_address_2").value = billing_address_2;
document.getElementById("shipping_city").value = billing_city;
document.getElementById("shipping_state").value = billing_state;
document.getElementById("shipping_postcode").value = billing_postcode;
}
Upvotes: 0
Reputation: 173
function copyTextValue() {
if(document.getElementById('check1').checked){
let text1 = document.getElementById('Name1').value;
document.getElementById('Name2').value = text1;
document.getElementById('Name3').value = text1;
}
else{
document.getElementById('Name2').value = "";
document.getElementById('Name3').value = "";
}
}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<input type="text" id="Name1" name="Name1">
<input type="text" id="Name2" name="Name2">
<input type="text" id="Name3" name="Name3">
<input type="checkbox" id="check1" name="check1" onclick="copyTextValue();"/>
</body>
</html>
Upvotes: 0
Reputation: 1
This is how I do for unchecking:
document.getElementById("Name2").value ="";
document.getElementById("Name3").value="";
Upvotes: 0
Reputation: 31920
function copyTextValue(bf) {
var text1 = bf.checked ? document.getElementById("Name1").value : '';
document.getElementById("Name2").value = text1;
document.getElementById("Name3").value = text1;
}
<input type="checkbox" name="check1" onchange="copyTextValue(this);" />
<input id="Name1"><input id="Name2"><input id="Name3">
Upvotes: 13