The function of your plugin is to trigger the event 'myEvent'. The first time you invoke the hilight() method but now the 'myEvent' event not bind to '#myDiv',so you cannot get the alert information. The second time you click, the '#myDiv' has bind 'myEvent' one time, so you get the alert information one time. The third time you click, the '#myDiv' has bind 'myEvent' two time, so you get two alert information.
To solve the problem, you should bind the 'myEvent' only once. $('#myDiv').bind('myEvent',function(e){ alert('event fired'); }); $('#myButton').click(function(){ $('#myDiv').hilight(); }) Allen123 wrote: > > Hi all, > > I am learning how to author jquery plug in and trying to fire a custom > event using trigger and bind. However, for some reason, when I click the > button first time, the custom event is not called. If I click again, the > event will execute once. If I click the third time, the event will execute > twice. Can you guys tell me what's wrong with my code? Really appreciate > your help. > > my plug in > > (function($) { > $.fn.hilight = function() { > > return this.each(function() { > > var $this = $(this); > //hightlight > > //call trigger > $this.trigger('myEvent'); > }) > }; > > })(jQuery); > > my html page > > $(document).ready(function() { > $('#myButton').click(function(){ > > $('#myDiv').hilight().bind('myEvent', function(e) { > alert('event fired'); > > }); > }) > }); > -- View this message in context: http://old.nabble.com/custom-event-firing-tp26524737s27240p26525336.html Sent from the jQuery General Discussion mailing list archive at Nabble.com.