dg85
dg85

Reputation: 490

Add a public function to a jquery plugin, to be called from another script

I'm using the Adobe Accessible Mega Menu plugin and am looking to extend this, (basically add a function that can be called from another script).

I've looked into extending jquery plugins, javascript closures and various other threads on the subject and I can see how this works

I can also see a bunch of public attributes and methods (ln 695 on) however attempting to call these merely returns the jquery object?

Equally adding a function and attempting to call that doesn't seem to work?

I've added a function called testFunction which fires an alert and should (i think) be called :

$("nav:first").accessibleMegaMenu("testFunction");

but no luck far..

Does anyone know how I can add a function to the above script, that can be called from another script?

https://codepen.io/anon/pen/VjzkqE

edit: solved - functions need to be accessed through the prototype obj as in :

nav.accessibleMegaMenu.AccessibleMegaMenu.prototype.customFunction(param);

Upvotes: 1

Views: 73

Answers (1)

Alaa M. Tekleh
Alaa M. Tekleh

Reputation: 7976

I have implemented this approach on a project by using extend function from jquery and prototype to extend infinitescroll plugin you can take this approach and implement it to your plugin I hope this would be useful for you as well

$.extend($.infinitescroll.prototype, {
    fucntion1: function() {
      //function implementation    
    },
    fucntion2: function() {
       //function implementaion
    }    
});

take a look for the question Best Way to Extend a jQuery Plugin

Upvotes: 1

Related Questions