I'm using boostrap 3 with modals. I've a button that opens a modal, in the modal there are some facet blocks. The view has ajax enabled, so when I click on a facet the page is not reload but are reloaded the content of the view AND facets blocks.

I've to execute some js functions when I click on facets links, I've added this js to my theme:

Drupal.behaviors.my_theme = {
    attach: function (context, settings) {
        jQuery('#facets a', context).click(function () {

The problem is that the alert is executed only on the first click. On second, third etc alert does not appear.

What's wrong?


Check to see if the ID has changed. If the content containing #facets is getting ajaxed in then the ID may have changed

Or you could try:

jQuery('body').on('click', '#facets a', function () {
  • Yes it works, thanks. The problem now is that, after the second click, "alert" is executed many times. On every click I've to append a loading-spinner to the html
    – arrubiu
    Jul 11 '18 at 10:40
  • However I found a trick, but the problem that the code is execute many times..
    – arrubiu
    Jul 11 '18 at 10:53
  • You could try adding .once like: jQuery('body').once('facet-clicked').on('click', '#facets a', function () {
    – Leigh
    Jul 11 '18 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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