I'm using prototype to attach event handlers to a number of form fields. The form fields are named "ContactAddress", "ContactCity", "ContactState", "ContactZip" as well as another set that begins with "ContactProfile". Now, the event needs to be able to tell the difference between when I'm updating a "Contact" field and a "ContactProfile" field so I have a data structure and code that works like this:
var geos = {
'Contact' : [
'Address', 'City', 'State', 'Zip'
],
'ContactProfile' : [
'Address', 'City', 'State', 'Zip'
]
};
// Called on page load
function watchGeo() {
for(prefix in geos) {
geos[prefix].each(function(item) {
$(prefix + item).observe('change', function() {ajaxgeolocate(prefix, item);});
})
}
}
// Doesn't actually geolocate anything yet, just tells me that the event works and what params got passed.
function ajaxgeolocate(prefix, suffix) {
alert(prefix + " " + suffix);
}
Upon loading the page, when I change one of the ContactProfile fields, it gives me an alert box that says "ContactProfile Address" (for example) like it should. However, if I change one of the Contact fields, it gives me an alert box that also says "ContactProfile Address". Curious, I inspected the field and found out that two event handlers had been added to all Contact fields, instead of just one. ContactProfile fields all have one event, as they should.
What's going on here?