digitalpencil digitalpencil - 1 year ago 85
jQuery Question

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 :


but no luck far..

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

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


Answer Source

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