4 $.fn.prepopulate = function(dependencies, maxLength, allowUnicode) {
7 Populates a selected field with the values of the dependent fields,
8 URLifies and shortens the string.
9 dependencies - array of dependent fields ids
10 maxLength - maximum length of the URLify'd string
11 allowUnicode - Unicode support of the URLify'd string
13 return this.each(function() {
14 var prepopulatedField = $(this);
16 var populate = function() {
17 // Bail if the field's value has been changed by the user
18 if (prepopulatedField.data('_changed')) {
23 $.each(dependencies, function(i, field) {
25 if (field.val().length > 0) {
26 values.push(field.val());
29 prepopulatedField.val(URLify(values.join(' '), maxLength, allowUnicode));
32 prepopulatedField.data('_changed', false);
33 prepopulatedField.change(function() {
34 prepopulatedField.data('_changed', true);
37 if (!prepopulatedField.val()) {
38 $(dependencies.join(',')).keyup(populate).change(populate).focus(populate);