Javascript function to extract address fields and add to hidden field?

By: Jason Cathcart | Asked: 01/17/2023
ForumsCategory: Code HelpJavascript function to extract address fields and add to hidden field?
Jason Cathcart asked 2 years ago
Formidable support suggested I reach out here. I am using the Premium Address Field option to capture International addresses, however the address gets stored in my custom field in an array format like this: {"line1":"7 Chome-14-15 Ginza","city":"Chuo City","state":"Tokyo","zip":"104-0061","country":"Japan"} I would like this stored in a more normal looking format like this: 7 Chome-14-15 Ginza Chuo City Tokyo 104-0061 Japan Formidable support suggested that someone here might be able to help with a custom JavaScript function to extract each field in the address field and add that to a hidden field in my form.

Is there someone here that can help with this?
1 Answers
Rob LeVineRob LeVine answered 2 years ago
472 = Field ID of your address field
field_intl_address = field key of your address field
field_hidden_address = field key of your hidden field
You might want to include "line2" below. I only added "line1" <script>
jQuery(document).ready(function($) {

	$("#frm_field_472_container").change(function() {
		const intl_address_field_prefix = 'field_intl_address_';
		
		var streetAddress = $("#" + intl_address_field_prefix + "line1").val();
		var city = $("#" + intl_address_field_prefix + "city").val();
		var province = $("#" + intl_address_field_prefix + "state").val();
		var postal = $("#" + intl_address_field_prefix + "zip").val();
		var country = $("#" + intl_address_field_prefix + "country").val();
		$("#field_hidden_address").val(streetAddress + " " + city + " " + province + " " + postal +  " "  + country);
	});
});
</script>
Jason Cathcart replied 2 years ago

Thanks so much - I'll give this a try!

Jason Cathcart replied 2 years ago

This code works great! Thanks!

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right