Change Image Alternative text on select box change Fields: 


Image Field - field_image
Selectbox Field - field_custom_language

Testcase: On selecting a different value from Selectbox, Alternative text of the image field should be changed.
Issue: Alternative text is not changed on ajax call.



    function custom_form_alter(array &amp;$form, FormStateInterface $form_state, $form_id) {
      $form['field_custom_language']['widget']['#ajax'] = [
        'callback' =&gt; 'AltChange',
        'event' =&gt; 'change',
        'wrapper' =&gt; 'custom-image-wrapper',
        'progress' =&gt; [
          'type' =&gt; 'throbber',
          'message' =&gt; t('Loading...'),
        ],
      ];
    }

    function AltChange(&amp;$form, FormStateInterface $form_state) {
      $form['field_image']['widget'][0]['#default_value']['alt'] = $form_state-&gt;getValue('field_custom_language');
      $form['field_image']['widget'][0] = ImageWidget::process($form['field_image']['widget'][0], $form_state, $form);
      return $form['field_image'];
    }
I don't think it can be done using AJAX with php, image field doesn't expose the alt as field with value to change it.
But there is a way to do it by using custom Javascript in an AJAX callback like the following:

/**
 * Implements hook_form_alter().
 */
function MODULENAME_form_alter(array &amp;$form, FormStateInterface $form_state, $form_id) {
  $form['field_custom_language']['widget']['#ajax'] = [
    'callback' =&gt; 'AltChange',
    'event'    =&gt; 'change',
    'wrapper'  =&gt; 'edit-field-image-wrapper',
    'progress' =&gt; [
      'type'    =&gt; 'throbber',
      'message' =&gt; t('Loading...'),
    ],
  ];
  // Add library.
  $form['#attached']['library'][] = 'MODULENAME/alter-alt-image';
}


and the callback function should be like:

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\InvokeCommand;
function AltChange(array &amp;$form, FormStateInterface $form_state) {
  // Get the language value.
  $lang_code = $form_state-&gt;getValue('field_custom_language')[0]['value'];
  $response = new AjaxResponse();
  $response-&gt;addCommand(new InvokeCommand(NULL, 'AltChange', [$lang_code]));
  return $response;
}


and Then create the library:
MODULENAME.libraries.yml

alter-alt-image:
  version: 1.x
  js:
    js/script.js: {}


and your Js script:
MODULENAME/js/script.js

(function($) {
  // Argument passed from InvokeCommand.
  $.fn.AltChange = function(argument) {
    // Set alt value to the passed arguments.
    $('input#edit-field-image-0-alt').attr('value', argument);
  };
})(jQuery);