I'm trying to add a method to a jQuery object that has the same name (but different parameter set) as another method.
What I've got so far:
jQuery.fn.insertBefore = function(elem, duration)
{
this.css("display", "none");
this.insertBefore(elem);
this.toggle(duration);
}
However, this code (specifically the this.insertBefore(where);
line) calls this same function, and not the jQuery insertBefore()
function, as desired. What do I need to do in order to add this function to the jQuery object, and have it overload (not overwrite) the existing function?
EDIT: Solution
(function ($)
{
var oldInsertBefore = $.fn.insertBefore;
jQuery.fn.insertBefore = function(elem, duration)
{
if (duration === undefined)
{
oldInsertBefore.call(this, elem);
return;
}
this.css("display", "none");
this.insertBefore(elem);
this.toggle(duration);
}
})(jQuery);
You back up original function before overwriting. Something like this:
(function($){
var oldInsertBefore = $.fn.insertBefore;
jQuery.fn.insertBefore = function(elem, duration)
{
oldInsertBefore.apply(this, arguments);
this.css("display", "none");
this.insertBefore(elem);
this.toggle(duration);
}
})(jQuery);
you can use $.sub()
(function($){
var sub$ = $.sub();
sub$.fn.insertBefore = function(where, duration) {
this.css("display", "none");
this.insertBefore(where);
this.toggle(duration);
}
/* you can use this plugin in this scope or you could also
return sub$ and call the plugin outside */
})(jQuery);
description of $.sub()
:
Creates a new copy of jQuery whose properties and methods can be modified without affecting the original jQuery object.
from http://api.jquery.com/jQuery.sub/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With