Merge pull request #22087 from agileware/CIVICRM-1887
[civicrm-core.git] / js / AlternateContactSelector.js
CommitLineData
1cef3745
CW
1CRM.$(function($) {
2 'use strict';
3
4 function assignAutoComplete(id_field, profileids) {
5 $('#' + id_field).on('change', function (event, data) {
6 var contactID = $(this).val();
7 CRM.api3('profile', 'get', {'profile_id': profileids, 'contact_id': contactID})
8 .done(function (result) {
9 $.each(result.values, function (id, value) {
10 $.each(value, function (fieldname, fieldvalue) {
a97d770e 11 $('#' + fieldname).val(fieldvalue).change();
103e73fb 12 $('[name="' + fieldname + '"]').val([fieldvalue]);
a97d770e 13 if ($.isArray(fieldvalue)) {
14 $.each(fieldvalue, function (index, val) {
15 $("#" + fieldname + "_" + val).prop('checked', true);
16 });
17 }
1cef3745
CW
18 });
19 });
20 }
21 );
22 });
23 }
24
25 $(CRM.form.autocompletes).each(function (index, autocomplete) {
26 assignAutoComplete(autocomplete.id_field, CRM.ids.profile || []);
27 });
28
29});
30