I am working on an autocomplete function using JavaScript for a form, and have run into a problem with the checkbox in my HTML file. The basic idea is that the user should be able to fill out their shipping name and zip code in a form fieldset, and then if they check off "Is billing info the same?", a JS function will be called that fills in the billing fieldset for billing name and zip code with the same shipping values. The checkbox is able to accomplish this, but I am unable to uncheck it to clear the billing info. Essentially, I cannot uncheck my checkbox. My code is below:
<form>
<fieldset>
<legend>Shipping Information</legend>
<label for ="shippingName">Name:</label>
<input type = "text" name = "shipName" id = "shippingName" required><br/>
<label for = "shippingZip">Zip code:</label>
<input type = "text" name = "shipZip" id = "shippingZip" pattern = "[0-9]{5}" required><br/>
</fieldset>
<input type="checkbox" id="same" name="same" onchange= "billingFunction()"/>
<label for = "same">Is the Billing Information the Same?</label>
<fieldset>
<legend>Billing Information</legend>
<label for ="billingName">Name:</label>
<input type = "text" name = "billName" id = "billingName" required><br/>
<label for = "billingZip">Zip code:</label>
<input type = "text" name = "billZip" id = "billingZip" pattern = "[0-9]{5}" required><br/>
</fieldset>
<input type = "submit" value = "Verify"/>
</form>
function billingFunction(){
var billName=document.getElementById("billingName");
var shipName=document.getElementById("shippingName");
var billZip=document.getElementById("billingZip");
var shipZip=document.getElementById("shippingZip");
var same=document.getElementById("same");
if (same.checked=true){
billName.value=shipName.value;
billZip.value=shipZip.value;
}
else{
billName.value="";
billZip.value="";
}
}
Aucun commentaire:
Enregistrer un commentaire