Hello,

I add a flag link in a node template page with :

flag_create_link( 'wishlist', $node->nid );

After that, I have a js script with the following content :

Drupal.behaviors.mymodule_view = function( context ) {
  // some behaviors attached to buttons / links / etc...
  //
  //

  // replace flag link with a background-image
  jQuery( ".flag-wishlist .flag").html("");
  jQuery( ".flag-wishlist .flag-throbber").remove();

  // some other behaviors attached to buttons / links / etc...
  //
}

Now when I click on the wishlist flag button, the flag is added / removed for the content, but all other behaviors are reattached to buttons, links, etc...

1. When I load the page, all is ok
2. I click on the wishlist flag button, behaviors are reattached to all. for example, I have a button to show / hide a div. When I click on this button, div is displayed and hidden (2 time action).
3. I click another time on the wishlist flag button, behaviors are reattached second time. A click on the show / hide div button show, hide and show the div (3 time action).

etc...

I don't understand why all behaviors are reattached each time... If anyone can explain to me what's my mistake... ?

Comments

quicksketch’s picture

Category: bug » support

You need to update your behaviors to search only the "context" of the page that was changed by the AJAX request. In your behavior code, include the context variable as the second parameter to all your jQuery() calls:

Drupal.behaviors.mymodule_view = function(context) {
  jQuery( ".flag-wishlist .flag", context).html("");
  jQuery( ".flag-wishlist .flag-throbber", context).remove();
}
titouille’s picture

Hi, thanks for your reply.

I used another trick to avoid the attachBehavior. I added "-processed" class for each binding and now all is ok.

example :

  $('#add-to-collection:not(.collection-processed)', context).addClass('collection-processed').bind( "click", displayCollections );
quicksketch’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

miche’s picture

quicksketch's "include the context variable as the second parameter" in comment 1 totally solved all my issues with a clicked flag interfering with my jquery.