Autofill fields on profile form based on previous fields I have a particularly long user profile form on my client's site. Some of those fields can be autofilled using information already entered (such as spouse last name). The filling out needs to be done dynamically on the page. How can I do this?

[Edit] 
Adding code for a problem that I experienced (anonymized):

function foo_profile_autofill_form_profile_node_form_alter (&amp;$form, &amp;$form_state){
    foreach(array('field_first_name', 'field_last_name',
            'field_spouse_first_name','field_spouse_last_name') as $fieldName){
            $form[$fieldName]['#attributes']['class'][] = "combname";
    }

    drupal_add_js(drupal_get_path('module', 'foo_profile_autofill').'/foo_profile_autofill.js');


}
You will have to use JavaScript to dynamically copy information from those fields. Depending on the structure of your form, you could also use a form_alter() to assign data- attributes to fields, which will reduce the number of lines of JS and make maintaining easier.

Here's sample code that should explain the idea::

JS:

$('input[data-sync]').change(function() {
  // Including the selector in data-attribute of the field will allow you
  // to have both - IDs and classes out of the box. This means you can
  // change the values of multiple fields, after only one change.
  $($(this).attr('data-sync-target')).val($(this).val());
});


hook_form_alter():

/**
 * Implements hook_form_alter().
 */
function MYMODULE_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  switch ($form_id) {
    case 'your-form-id':
      $form['some_field']['#attributes'] = array(
        'data-sync' =&gt; TRUE,
        // You would use this to target an element with ID.
        'data-sync-target' =&gt; '#target-element-id',
        // OR you can use this to target multiple elements with this class.
        'data-sync-target' =&gt; '.target-element-class',
      );
      // If necessary, you can add custom CSS classes to elements like so:
      $form['some_field']['#attributes']['class'][] = 'your-special-class';
      break;
  }
}