0

How can I call a function defined as JQuery plugin option parameter in my Base Script?

Plugin

$.fn.myPlugin = function(){
   var config = $.extend({
    read:function(){}

})
config.read.call(this);
     
}

Sample Usage

$("#someElement").myPlugin({

     read: function myFunc(){
            //some code
          }
});

function anotherFunc(){
     myFunc();
    //here, why I cant call the function myFunc defined as one of my option parameter for Jquery Plugin
}
5
  • The function you are passing to read: is scoped to the myPlugin init object - you would normally use an anonymous (unnamed) function here as the name is unusable elsewhere (due to its scope): read: function() {.... Use a global function: function myFunc() { .. read: myFunc. Commented Nov 14, 2022 at 10:43
  • Oh I see, but is there no other way, I can call myfunc function? Commented Nov 14, 2022 at 10:49
  • The alternative is to expose/export your config. This answer might help you to create jquery-plugin-style options: stackoverflow.com/a/6457128/2181514 -> $("#id").myPlugin("read") See also this answer: stackoverflow.com/a/25343333/2181514 Commented Nov 14, 2022 at 10:56
  • Will it be myPlugin.config.read.call(this)?. Is it correct? Commented Nov 14, 2022 at 11:01
  • No / depends on how you store your config. That would be a global config $.fn.myPlugin.config.read... but if you have a different config per call to myPlugin then you need to store it on the element itself. See the above links. Commented Nov 14, 2022 at 11:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.