I have added callback to my jQuery plugin.
$.fn.myPlg= function(options, callback) {
if(callback) {
//do stuff
}
}
How now call this callback from jQuery e.g
$(document).myPlg( function(){
// how to call callback?
});
I have added callback to my jQuery plugin.
$.fn.myPlg= function(options, callback) {
if(callback) {
//do stuff
}
}
How now call this callback from jQuery e.g
$(document).myPlg( function(){
// how to call callback?
});
This will cause the callback function to execute:
$.fn.myPlg= function(options, callback) {
if(callback) {
callback();
}
}
As Samich said, you should use an options object, even if the callback is your only option. That way you can add more options easily.
doing it that way would look like this:
$.fn.myPlg= function(options) {
if(options.callback) {
options.callback();
}
}
and
$(document).myPlg({
callback: function() {
// callback logic here
}
});
In your case it will be second parameter:
$(document).myPlg({option1: 'a', option2: 'b'}, function(){
// callback logic here
});
But please note that you need to call callback inside of plugin definition, not usage. I mean in the first part of your samples.
It's better to include it in options:
$.fn.myPlg= function(options) {
if(options.callback) {
//do stuff
}
}
$(document).myPlg({
option1: 'a',
option2: 'b',
callback: function() {
// callback logic here
}
});
myPlg selector to run the code in the plugin? if(options.callback) { //here }callback but I don't think that it will be good solution.