This should be an easy one.
How to write a jquery function that gets the ‘calling’ object and some params.
My function code:
new function($) {
$.fn.addcolor = function(param1, param2, param3, param4) {
// here i would like to retrieve the 'caller' object (div.foo in this case)
}
} (jQuery);
My code that ‘calls’ the function:
$('div.foo').click(function() {
$(this).addcolor(1, 2, 3, 4);
});
I can get the params no problemo, but I want to get the content of the div.foo in the function and add some content it.
You’re looking for
this.jQuery plugins in
$.fnare normal methods on the$prototype. (jQuery assigns$.fnto$.prototype)Like any other method, you can get the context in which it was invoked using
this.