I am having trouble getting the Billing Address to copy over the Shipping Address using jQuery. I have successfully done this using a plain-jane form with no custom jQuery elements. But when I add the custom UI to the checkbox, it seems to break the code. I have tried several code changes but none of them are working.
When a user clicks on "My billing address is the same as my shipping address", nothing is happening. Here is my jQuery code:
<script type="text/javascript">
$(document).ready(function(){
$('#check-address').click(function(){
if($('#check-address').attr('checked')){
$('#address-field1').val($('#address-field').val());
$('#city-field1').val($('#city-field').val());
$('#zip-field1').val($('#zip-field').val());
var state = $('#state-field option:selected').val();
$('#state-field1 option[value=' + state + ']').attr('selected','selected');
} else {
//Clear on uncheck
$('#address-field1').val("");
$('#city-field1').val("");
$('#zip-field1').val("");
$('#state-field1 option[value=Nothing]').attr('selected','selected');
};
});
});
</script>
Any help is greatly appreciated!
Your billing address is often the same as your shipping address, but they may be different they may be different if you've moved recently or used a post office box. Here's how to verify your billing address: Your credit card statements show your billing address if you receive the statements by postal mail.
Whenever the checkbox is checked, the code should automatically copy the values from Shipping Name and Shipping Zip into the Billing Name and Billing Zip. If the checkbox is unchecked, the Billing Name and Billing Zip should go blank.
An invoice address is the legal address of the buyer or the address where they receive correspondence. It is differentiated from a shipping address (or delivery address), which is the address where goods or services are to be delivered.
Demo of options
$('#check-address').attr('checked')
should could be
$('#check-address').is(':checked')
this.checked
seems to be the simplest.
You are using jquery 1.7 . You should use prop instead of attr
$('#check-address').prop('checked')
$(elem).attr('checked') // returned true (Boolean) prior to 1.6
$(elem).attr('checked') // now returns "checked" (string) versions after 1.6
$(elem).prop('checked') // now returns true (Boolean) versions after 1.6
The plugin you're using to prettify your checkbox (or whatever it does), is replacing the <input type="checkbox"/>
with a div
, and simulating events on the checkbox.
It doesn't propagate a click
event, but it does propagate a change
, so you should listen for that instead;
$('#check-address').change(/* function */);
To improve your code you might want to substitute the check for attr('checked')
with prop('checked')
, but your code will still work regardless. Reasons for this are outlined on the jQuery documentation for prop()
; http://api.jquery.com/prop
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With