I am trying to create a jQuery plugin.
$.fn.examplePlugin = function(callback) {
$.item = $(this);
$(this).hide('fast',function () {
callback('bla');
});
function show(a) {
alert(a);
}
}
and use
$('form').examplePlugin(function(data)) {
this.show(data); // need to return alert 'bla'
});
need to return alert ‘bla’
After your edits, your code almost does what you mean to do. There are only a couple of problems:
What is the purpose of this line?
You’re creating a field in the jQuery object (alias
$) that is referencing an element. If you call your plugin N times, that field will be overwritten N times. Whatever you were trying to accomplish, this is not the way to do it.Your function
showis only available inside the closurefunction(callback) { ... }, you can’t access it from outside. Callingthis.showwill do nothing, and you shouldn’t try to add members tothisbecause it refers to a DOM element.For a “proper” way of doing it, I’d suggest reading some tutorials on plugin authoring, but as a quick workaround, why don’t you pass the
showfunction as argument to the callback also?