I'm completely clueless about the problem. I have tried to google but have not found any similar issues.

My AJAX callbacks work only on the select field type, but I want to trigger ajax on textfield input.

What I did: Created content type and added textfield 'field_sku' using the admin interface.

In [mymodule].module file added hook_form_alter and added ajax to field widget:

$form['field_sku']['widget']['#ajax'] = [
  'event' => 'change',
  'callback' => 'myAjaxCallback',
  'wrapper' => 'edit-title-wrapper',

The hook is triggering normally.

Ajax is never triggered by input in that field, and no drupal ajax classes are added to the markup.

If I'm changing field_sku to select box by deleting this field and adding new one with same machine name, everything starts working as expected. But I need it to be textfield and trigger ajax on user input.

As far as i know, there should not be a problem, where did i go wrong?

  • 1
    I am not posting this as an answer because I am not certain. But, I think the change event only fires for text fields when focus changes. Did you mean input? I also think you may not specify anything and use the default.
    – cilefen
    Feb 25 at 14:37
  • Welcome to Drupal Answers :) @cilefen is right, Drupal basically adds on to the beginning of the event, so change becomes onchange, which fires on blur (focus lost). input (oninput) is probably perfect for what you need. keyup would also work
    – Clive
    Feb 25 at 20:24
  • Hey! I tried all the mentioned events but nothing worked, and considering that the field HTML structure has not changed after Ajax added to widgets in the hook. the event type is not related to the problem Feb 26 at 0:31
  • Is ['field_sku']['widget'] the correct form API element on which to attach the callback?
    – cilefen
    Feb 26 at 0:50
  • yes, this is correct according to drupal documentation Mar 9 at 19:21


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.