0

I have 3 field collection 1st is parent in that having other 2 is child field collections.

  • parent FC: Student Info
  • child FC 1: English Info
  • child FC 1: Maths Info

Set field setting "unlimited" to parent field collection(i.e Student Info).

Now I want hide 2 child field collection (i.e English Info and Maths Info) on click of "Add another item" I have hide child FC using jquery

For exmaple:

jQuery('.child1').hide();

jQuery('.child2').hide();

Child field collection hide initially, but if I click "Add another item" button then jquery removed and child FC shows. I have tried to bind click event in jquery but it's not working.

2
  • Can you provide more details on the JS code you're using? In general, it should reside under attach function under drupal behavior because it gets called with each Ajax call Commented Sep 6, 2018 at 21:03
  • I have created js file and attach that file in form alter hook like this, $form['#attached']['library'][] = 'example/exampleJS'; example.js is my file name. In JS file, I'm using below code to hide child field collection. jQuery('#node-student-form .field--type-field-collection .field--name-field-child-english-info').css("display","none"); // Child FC 1 jQuery('#node-student-form .field--type-field-collection .field--name-field-child-maths-info').css("display","none"); // Child FC 2
    – user66981
    Commented Sep 7, 2018 at 4:06

1 Answer 1

1

Can you replace the code in your JS file with the following:

(function ($, Drupal, drupalSettings) {

  'use strict';

  Drupal.behaviors.myModuleName = {
    attach: function (context, settings) {
        $('#node-student-form .field--type-field-collection .field--name-field-child-english-info').css("display","none"); // Child FC 1 
        $('#node-student-form .field--type-field-collection .field--name-field-child-maths-info').css("display","none"); // Child FC 2
    }
  };

})(jQuery, Drupal, drupalSettings);

This is based on this article
The attach() function ensures that your code runs each time an Ajax call is made

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.