I have a process designed to change concatenate separate address fields entered by a user into a single hidden field to be used by Google Maps to drop a pin on the location.
The Event Listeners responsible for the concatenation are functioning properly, but when I try to chain a listener to the hidden field to execute the pin placement, the function isn't executed.
Here is the code I am using:
$(document).ready(function initMap() {
...
function join_address() {
var address = document.getElementById('id_form5-address1').value;
var city = document.getElementById('id_form5-city').value;
var state = document.getElementById('id_form5-state').value;
var zip = document.getElementById('id_form5-zip').value;
document.getElementById('id_jointAddress').value = address+", "+city+", "+state+", "+zip;
}
document.getElementById("id_form5-city").addEventListener("change", join_address);
document.getElementById("id_form5-state").addEventListener("change", join_address);
document.getElementById("id_form5-zip").addEventListener("change", join_address);
function codeAddress() {
console.log("function engaged")
var address = document.getElementById("id_jointAddress").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
document.getElementById("id_jointAddress").addEventListener("change", codeAddress);
...
}
"Function engaged" never even prints to console, so I am assuming that it is the listener that is constructed wrong, or is in the wrong place. Can anyone assist?
Edit: Here is the html object in question:
<input type="hidden" name="jointAddress" id="id_jointAddress">
If I unhide the element, and type in the field directly it triggers the listener. I think the "change" listener does not respond to changes made through Javascript. Does anyone know of a solution that will work for me?