I have an issue ( http://drupal.org/node/1698152 ) where on a multipage form, on the first "page", there is a field collection with an "add another" button. And it does. But it shows the new set of fields below those that have already been filled out. The client wants those previous sets hidden. Each "set" is actually a un-ID'd TR. I found that by going further up in the DOM to the table, which has an ID, then iterating its TR collection, I can hide the previous rows. The current problem is that after my jquery finishes executing, and hiding, the filled out rows, the "add another" process goes ahead re-reveals them! *eye roll* Not your fault. But I was searching for a work-around, noticed your module, and thought it might be a solution. Maybe I could move my jquery bit *after* whatever jquery the "add another" button is executing. Your example seems pretty cut & dried, so I maybe I am just dense. :-) Where am I not implementing the module correctly?

(function ($) {

  Drupal.behaviors.sva_pay = {
    attach: function (context, settings) {

	  // Let this run after all other behaviors (default weight is 0)
	  Drupal.behaviors.sva_pay.weight = 999; // (because I want it to happen after everything else

      if ( $('#edit-field-entry-list-und-add-more').length ) {

        $('#edit-field-entry-list-und-add-more').mousedown(function(){

          var last_row = $("#field-entry-list-values tbody tr").length;

          /*
          ** The title is a TR-TH object, so no need to worry about that. Hide all
          ** the TR-TD entry rows, except the just added last row, as they still
          ** need to be submitted.
          */
          for ( idx = 0; idx <= last_row ; idx++ ) {

            action_string = "#field-entry-list-values tbody tr :nth-child(" + idx + ")"
            $(action_string).hide();

          }
        });
      }
    }
  };
})(jQuery);

Thanx,

P

Comments

Preston McMurry’s picture

Precedence of some common modules: http://drupal.org/node/110238

Even after changing to 9999, or 999999999, or whatever, still can't get my jquery to execute after that of the default 'add another' jquery behavior ...

donquixote’s picture

Yeah, big thinking mistake in your code :)
The weight setting needs to be outside of the attach function.
Otherwise it is already too late when it is called.

(function ($) {

  Drupal.behaviors.sva_pay = {
    attach: function (context, settings) {
      ...
    }
  };

  // Let this run after all other behaviors (default weight is 0)
  Drupal.behaviors.sva_pay.weight = 999; // (because I want it to happen after everything else
})(jQuery);

Or what about:

(function ($) {

  Drupal.behaviors.sva_pay = {
    attach: function (context, settings) {
      ...
    },
    // making use of behavior_weights module
    weight: 999
  };
})(jQuery);
Preston McMurry’s picture

Status: Active » Closed (fixed)

Thanx for the quick response! :-)

I was just coming back in here to let you know that I had found another approach that seems to work:

(function ($) {

  $(document).ajaxComplete(function(e, xhr, settings) {

    if ( $('#edit-field-entry-list-und-add-more').length ) {

        var last_row = $("#field-entry-list-values tbody tr").length;

        /*
        ** The title is a TR-TH object, so no need to worry about that. Hide all
        ** the TR-TD entry rows, except the just added last row, as they still
        ** need to be submitted.
        */
        for ( idx = 0; idx < (last_row-1) ; idx++ ) {

          action_string = "#field-entry-list-values tbody tr:eq(" + idx + ")"

          $(action_string).hide();
        }
    }
  });

The key was changing to ajaxComplete. (And changing nthChild to eq.) Obviously that has nothing to do with your module, but I will definitely keep your pointer in mind for future reference! :-)

donquixote’s picture

Sure, you can always use things like
jquery(function($){..}), or this ajaxComplete thing (actually, never tried this one, but probably the reason why it works is the same)
and hope that this fires before Drupal stuff.

Still it would be interesting to get some feedback if the behavior weight thing would work for you :)