kirankumar
kirankumar

Reputation: 197

Assign event in plugin

I was developed a plugin that for assign contextmenu. Html Code:

<ul id='list1'>
 <li class='item'>Items</li>
 <li class='item'>Items</li>
 <li class='item'>Items</li>
  ...
</ul>

jQuery plugin code:

$(function(){
 $.fn.cnxtmenu=function(options){
   this.bind('contextmenu',function(){
    //To display menu
    });
  }
});

Usage of plugin:

$('#list1').cnxtmenu(options);
$('.item').cnxtmenu(options);

My problem is plugin code is executing many times.

Upvotes: 0

Views: 52

Answers (1)

davids
davids

Reputation: 6371

My first guess is, as you are attaching the handler to several elements and to their container as well, the event is bubbling up and the handler is executed twice. So, in your bind, try this:

// Remember to pass 'ev' to the handler
this.bind('contextmenu',function(ev){
    ev.stopPropagation();
    //To display menu
});

As I told you, is my first guess. I'd need the code where you trigger the contextmenu event to give a more accurate answer. If your code is executing, somewhere you must have $(something).trigger('contextmenu')

Upvotes: 1

Related Questions