diff --git a/core/modules/system/system.js b/core/modules/system/system.js index a8e8dc9..e66f244 100644 --- a/core/modules/system/system.js +++ b/core/modules/system/system.js @@ -9,23 +9,32 @@ */ Drupal.behaviors.copyFieldValue = { attach: function (context, settings) { - function updateTarget (e) { - var targetValue = $(e.target).val(); - // Get the list of target fields. - var targetIds = settings.copyFieldValue[sourceId]; - for (var delta in targetIds) { - if (targetIds.hasOwnProperty(delta)) { - var targetField = $('#' + targetIds[delta]); - if (targetField.val() === '') { - targetField.val(targetValue); - } + // Initialize source->target mapping or grab exisiting. + // This is basically a copy of settings but decoupled from the global scope. + this.map = this.map || {}; + for (var sourceId in settings.copyFieldValue) { + if (settings.copyFieldValue.hasOwnProperty(sourceId)) { + var $source = $('#' + sourceId).once('copy-field-values'); + if ($source.length) { + // Add the behavior to update target fields on blur of the primary + // field. Using $.proxy() so 'this' references the behavior and + // not the element triggering the handler. + $source.bind('blur', $.proxy(this.updateTarget, this)); + this.map[sourceId] = settings.copyFieldValue[sourceId]; } } } - for (var sourceId in settings.copyFieldValue) { - if (settings.copyFieldValue.hasOwnProperty(sourceId)) { - // Add the behavior to update target fields on blur of the primary field. - $('#' + sourceId).once('copy-field-values').bind('blur', updateTarget); + }, + updateTarget: function (e) { + var targetValue = $(e.target).val(); + // Get the list of target fields. + var targetIds = this.map[e.target.id]; + for (var id in targetIds) { + if (targetIds.hasOwnProperty(id)) { + var $targetField = $('#' + targetIds[id]); + if ($targetField.val() === '') { + $targetField.val(targetValue); + } } } }