Wednesday, November 25, 2009

Re: [jQuery] custom event firing

i usually use something like this when coding a jQuery plugin.

(function() {
  var newMethods = {
// function start
      AddTo : function(options) {
        var defaults = {
// set default option values here
        };
        var options = $.extend(defaults, options); // access any options below this line
        var obj = $(this);
      }, // you seperate multiple functions by a , character
// function end
// function start
     RemoveFrom : function(options) {
        var defaults = {
// set default option values here
        };
        var options = $.extend(defaults, options); // access any options below this line
        var obj = $(this);
     }
// function end
  };
  jQuery.each(newMethods, function(i) {
    jQuery.fn[i] = this;
  });
})();



On Thu, Nov 26, 2009 at 8:06 AM, Allen123 <chiupin.chen@gmail.com> 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-tp26524737s27240p26524737.html
Sent from the jQuery General Discussion mailing list archive at Nabble.com.


No comments: