r/JavaScriptTips • u/htmlcssjss • Jun 07 '21
New coder, need help
Hi,
I am learning JavaScript and a task I am working on is to make an auto complete function to automatically fill in a form with input from another form upon checking a checkbox.
Here is the function I wrote:
var shippingName = document.getElementById('shippingName');
var shippingZip = document.getElementById('shippingZip');
function billingFunction(){
if(document.getElementById('same').checked){
document.getElementById('billingName').value = shippingName.value;
document.getElementById('billingZip').value = shippingZip.value;
}
}
And here is the HTML:
<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>Please let me know how I can fix this function. Currently the function works in codepen but not in my browser (chrome).
1
Upvotes
1
u/Satanacchio Jun 09 '21
I'm a simple man, I see var, I downvote