V
V
Vladimir Singhtlov2016-08-30 10:51:33
Drupal
Vladimir Singhtlov, 2016-08-30 10:51:33

Why does the event occur 2 times when the button is clicked?

(function($) {

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

      $('.icon_comments').bind('click', function(){
        var nids = $(this).closest('.mainlist-item').attr('about').split('/');
        nid = nids[nids.length - 1];
        if (!$(this).attr('data-comments')){
          $.post(Drupal.settings.basePath + 'lk/comments', {'nid': nid}, nodeDetails);
               $(this).attr('data-comments', true).addClass('open');
        }else{
          $(this).toggleClass('open');
        }   
      });

    }
  };

......

})(jQuery);

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
andead, 2016-08-30
@andead

Use once, context and read the documentation :
$('.icon_comments', context).once().bind('click'

V
Vitaly, 2016-08-30
@vshvydky

do return false after your code, maybe you have a second event or have .icon_comments nesting

A
amutylo, 2016-09-08
@amutylo

I sometimes do this in your example.
$('.icon_comments').unbind('click').click(function(){ ....
one click is obtained

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question