r/JavaScriptTips • • 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

4 comments sorted by

1

u/Hombrebestial Jun 07 '21

MDN says the checked attribute is not used to check if the input is toggled. You might want to use value instead in your conditional if statement. Check it out here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox

1

u/Satanacchio Jun 09 '21

I'm a simple man, I see var, I downvote

1

u/ColourfulToad Jun 14 '21

I know it's "just Reddit" but I'd argue against downvoting brand new coders asking for help for not knowing ES6 features. Perhaps suggest and explain or link to explanations of what these features are instead.

1

u/Satanacchio Jun 14 '21

Its a joke, dont mind me