Form field not show on Ajax Callback even the callback is called

I have a form with one shown field of type select with ajax callback to show another form field dynamically of type select and with ajax callback again, the problem is second field ajax callback should show a third field dynamically but nothing is shown. here is my code

public function buildForm(array $form, FormStateInterface $form_state) {     $controller = new Place_AdsController();     $terms = $controller->content();      $form['#attached']['library'][] = 'Place_Ads/general';     $form['level1value'] = array(         '#type' => 'hidden',     );     $form['level1'] = array(         '#type' => 'select',         '#description' => 'Please select level one term',         '#options' => $terms,         '#attributes' => array(             'class' => array(                 'mycategory'             ),         ),         '#ajax' => array(             // Function to call when event on form element triggered.             'callback' => '::level1SelectedCallback',             // Effect when replacing content. Options: 'none' (default), 'slide', 'fade'.             'wrapper' => 'ajax-wrapper2',             'effect' => 'fade',             // Javascript event to trigger Ajax. Currently for: 'onchange'.             'event' => 'change',             'progress' => array(                 // Graphic shown to indicate ajax. Options: 'throbber' (default), 'bar'.                 'type' => 'throbber',                 // Message to show along progress graphic. Default: 'Please wait...'.                 'message' => NULL,             ),         ),     );     $form['ajax_wrapper2'] = [         '#type' => 'container',         '#attributes' => ['id' => 'ajax-wrapper2'],     ];     $form['ajax_wrapper3'] = [         '#type' => 'container',         '#attributes' => ['id' => 'ajax-wrapper3'],     ];      if ($form_state->getValue('level1') != false) {         $form['ajax_wrapper2']['level2'] = array(             '#type' => 'select',             '#description' => 'Please select level two term',             //'#options' => '',             '#attributes' => array(                 'class' => array(                     'mycategory'                 ),             ),             '#ajax' => array(                 // Function to call when event on form element triggered.                 'callback' => '::level2SelectedCallback',                 // Effect when replacing content. Options: 'none' (default), 'slide', 'fade'.                 'wrapper' => 'ajax-wrapper3',                 'effect' => 'fade',                 // Javascript event to trigger Ajax. Currently for: 'onchange'.                 'event' => 'change',                 'progress' => array(                     // Graphic shown to indicate ajax. Options: 'throbber' (default), 'bar'.                     'type' => 'throbber',                     // Message to show along progress graphic. Default: 'Please wait...'.                     'message' => NULL,                 ),             ),         );     }     if ($form_state->getValue('level2') != false) {         //$form['ajax_wrapper3'][0]['#suffix'] = '</div>';         $form['ajax_wrapper3']['level3'] = array(             '#type' => 'select',             '#description' => 'Please select level three term',             //'#options' => '',             '#attributes' => array(                 'class' => array(                     'mycategory'                 ),             ),             '#ajax' => array(                 // Function to call when event on form element triggered.                 'callback' => '::level3SelectedCallback',                 // Effect when replacing content. Options: 'none' (default), 'slide', 'fade'.                 'wrapper' => 'ajax-wrapper4',                 'effect' => 'fade',                 // Javascript event to trigger Ajax. Currently for: 'onchange'.                 'event' => 'change',                 'progress' => array(                     // Graphic shown to indicate ajax. Options: 'throbber' (default), 'bar'.                     'type' => 'throbber',                     // Message to show along progress graphic. Default: 'Please wait...'.                     'message' => NULL,                 ),             ),         );     }      return $form; }  public function level1SelectedCallback(array &$form, FormStateInterface $form_state) {     // Instantiate an AjaxResponse Object to return.     $ajax_response = new AjaxResponse();     $controller = new Place_AdsController();     $terms = array();     if ($form_state->getValue('level1') != false) {         $val = $form_state->getValue('level1');         $terms = $controller->getTerms($val);      }      $form['ajax_wrapper2']['level2']['#options'] = $terms;     $ajax_response->addCommand(new HtmlCommand('#ajax-wrapper2', $form['ajax_wrapper2']['level2']));     // We can still invoke the change command on #edit-user-name so it triggers Ajax on that element to validate username.     $ajax_response->addCommand(new InvokeCommand('#ajax-wrapper2', 'change'));     $ajax_response->addCommand(new InvokeCommand('#level2', 'change'));      return $ajax_response; }  public function level2SelectedCallback(array &$form, FormStateInterface $form_state) {      $ajax_response = new AjaxResponse();     $controller = new Place_AdsController();     $terms = array();     if ($form_state->getValue('level2') != false) {         $val = $form_state->getValue('level2');         $terms = $controller->getTerms($val);      }      $form['ajax_wrapper3']['level3']['#options'] = $terms;     $ajax_response->addCommand(new HtmlCommand('#ajax-wrapper3', $form['ajax_wrapper3']['level3']));     // We can still invoke the change command on #edit-user-name so it triggers Ajax on that element to validate username.     $ajax_response->addCommand(new InvokeCommand('#ajax-wrapper3', 'change'));     $ajax_response->addCommand(new InvokeCommand('#level3', 'change'));      return $ajax_response;  }  public function level3SelectedCallbackold(array &$form, FormStateInterface $form_state) {     return null; } 
Sponsored by SupremePR
This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Form field not show on Ajax Callback even the callback is called

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.