views:

89

answers:

3

I am learning how to write jQuery plugin. So why I am doing what I am doing does not matter.

I writing a plugin called live2 which does nothing else but internally calls live method.

(function($) {

    $.fn.live2 = function() {

        /* if there are no elements then just return */
        if (!this.length) return this;

        return this.each(function() {
            var $this = $(this);

      jQuery.fn.live.apply(this, arguments);

        }); // end of return this.each(function())
    }; // end of plugin
})(jQuery);

Above code should be invoked just live any live method. Instead of live use live2.

$('#container').live2('click',function(){
return false;
})

But the plugin is not working. Any idea what the fix should be.

A: 

EDIT: I think you just need to do this:

$.fn.live2 = function() {
    this.live.apply(this, arguments);
};
Jason Orendorff
that did not work.
Nadal
+1  A: 

live only works with a selector, so

$('#something.something').live(...) will work

$(this).live(...) is not supposed to work (according to documentation)

Your plugin example looks quite ok. Id change this two things: 1. this into $this 2. return after calling each

And live is not a good function to do the experiments with. Try toggle

like this:

(function($) {

    $.fn.toggle2 = function() {

        /* if there are no elements then just return */
        if (!this.length) return this;

        this.each(function() {
            var $this = $(this);
            $this.toggle(arguments);
        }); 

        return this;

    }; // end of plugin
})(jQuery);
naugtur
A: 

Are you missing a $ in this line:

  jQuery.fn.live.apply(this, arguments);

Therefore,

  jQuery.fn.live.apply($this, arguments);
ndp