Webform hook field text element I Use Drupal 8.8 with Webform 8.x, in a simple webform with a select list element and a text field element, I need to update a text field element with values resulting from database queries when the user chooses an option from the listbox.

I would like to accomplish this with a PHP code in a custom mymodule. How can I intercept the user's action when selecting a value from the select list?

Besides intercepting the event I also need to know the content of the choice that the user has made on the select list.

How can it be done? Can anyone give me an example?
There are more than one way to do what you are  asking note both these will require you to create a custom module. (I assume you know how to do this or can google it)

here need to change to your form id, and your field names.. 

function MYMODULE_form_alter(&amp;$form, FormStateInterface $form_state, $form_id) {
    // change to your form id
  if ($form_id == 'webform_submission_contact_add_form') {
    //dump($form['elements']);
    $form['elements']['select_me']['#ajax'] = [
      'callback' =&gt; 'MYMODULE_callback_ajax',
      'event' =&gt; 'change',
      'wrapper' =&gt; 'changethis',
      'method' =&gt; 'replace',
    ];


   $form['elements']['TEXTFIELDNAME']['#prefix'] = '&lt;div id="changethis"&gt;';
   $form['elements']['TEXTFIELDNAME']['#suffix'] = '&lt;/div&gt;';  
 }
}
// ajax call back 
function MYMODULE_callback_ajax(&amp;$form, FormStateInterface $form_state) {
  $value_selected = $form_state-&gt;getValue('select_me');
  if (!empty($value_selected)) {
    // Here do queries then set the value of your text field.

    $form['elements']'TEXTFIELDNAME']['#value'] = $value_selected;
    return $form['elements']'subject'];
  }
}