1

There is an ajax callback which occurs on change of a field in my drupal 8 form. This populates a wrapper with information from the server. however I also want to excecute a jquery command to make the visibility of another field be visible after that . How can this be done?

I tried this $form['ajax-wrapper']['#attributes'] = array('visibility'=>'initial');

but that doesnt have any effect I need to be able to run this jquery command or this javascript but dont know how to trigger it after the ajax

document.getElementById( 'ajax-wrapper' ).style.visibility = 'initial';

           $("#ajax-wrapper").show();

1 Answer 1

3

You need to read the documentation as you are conflating the idea of ajax and what the AJAX capability of Form API is. A form item needs to add an #ajax item to perform ajax callbacks (php), and those functions can trigger AJAX commands some of which can trigger external JS functions.

On top of that, your actual JS code needs to conform to Drupal JS / behavior format so that it is leveraged by Drupal properly. The minimum here is it at least has to be enclosed in an anonymous function to prevent a jQuery conflict:

/**
 * @file
 */
(function ($) {

  "use strict";

  // All the JavaScript for this file.
  $("#ajax-wrapper").show();
  // other code or behaviors

})(jQuery);

Plus, if you have form fields that are dependent on other fields as far as being visible, you might be able to just use #states.

Here are resources:

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.