Score:0

Webform address killing ajax?

om flag

How do I attach an Ajax behavior to my country field (that already has a dropdown behavior).

I have a simple form defined using the webform module. enter image description here

In an implementation of hook_webform_alter I have the following code that works...

$form['elements']['business_e_mail']['#ajax'] = [
    'callback' => 'ajax_callback',
    'wrapper' => 'opt-in-wrapper',
    'disable-refocus' => FALSE, 
    'event' => 'change',
  ];

However, if I attempt to attach the #ajax behavior to the country field which is really what I need, I get no response. (e.g., the ajax callback is not called)

Code that does not work

$form['elements']['country_code']['#ajax'] = [
    'callback' => 'ajax_callback',
    'wrapper' => 'opt-in-wrapper',
    'disable-refocus' => FALSE,
    'event' => 'change',
  ];

The only seemingly relevent difference between the two fields is that the working field is a simple email address.

The country_code is an address field that also has a behavior attached to it (e.g., it is a dropdown.

Any help would be much appreciated!

enter image description here

Jaypan avatar
de flag
What does the unaltered `$form['elements']['country_code']` look like in the form definition? You may need to attach the `#ajax` to a child of that.
sea26.2 avatar
om flag
@jaypan Thanks for jumping into this one... The form is pretty much boilerplate from the webform module. ` country_code [ #type #title #webform_key #webform_plugin_id ... ` Interesting idea you've got... not sure how effective it will be because I need to find an element that changes - right?
sea26.2 avatar
om flag
I would need to find a child that is modified, or maybe not? `country_code [ #title #webform_key #country_code__options ]`
Jaypan avatar
de flag
I don't use webform myself. But form elements in Drupal often have children form elements, and you need to attach the #ajax to the actual form element (select, input) as it won't work when attaching it to a parent. This indicates to me that you've attached it to a parent, not the child.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.