Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9010947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:35:52+00:00 2026-06-16T02:35:52+00:00

I want to create a jQuery plugin that has both methods and callbacks, methods

  • 0

I want to create a jQuery plugin that has both methods and callbacks, methods work but I can’t get the callbacks working, the scope of the callbacks confuses me,

(function($)
{
    var methods = {

        create: function(options) {

            var defaults = {
                width: 320,
                height: 240,
                background: '#000',
                onstop: function(){}
            };

            var options = $.extend(defaults, options);

            return $(this).each(function(i) {

                console.log('create');

            });

        },
        stop: function(options) {

            var defaults = {
                onstop: function(){}
            };

            var options = $.extend(defaults, options);

            return $(this).each(function(i) {

                console.log('stop');
                options.onstop.call();

            });
        }
    };

    $.fn.myplugin = function(option) {

        if (methods[option]) {
            return methods[option].apply( this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof option === 'object' || ! option) {
            return methods.create.apply(this, arguments);
        } else {
            $.error('Method ' +  option + ' does not exist in jQuery.plugin');
        }    
    };

})(jQuery);

so in the <script>:

$(document).ready(function(){

    $('#start').click( function(){
        $('#myvideo').myplugin('create', { onstop: function(){ console.log('on stop'); } });
    });

    $('#stop').click( function(){
        $('#myvideo').myplugin('stop');
    });

});

basically it seems I want to make the onstop: function(){} global to the plugin

/* ======== updated code (see comments) ======== */

(function($)
{

    var callbacks = {
        onready: $.Callbacks("once memory unique").add(function() {
            //console.log('onready');
        }),
        ondeny: $.Callbacks("unique").add(function() {
            //console.log('ondeny');
        }),
        onerror: $.Callbacks("unique").add(function() {
            //console.log('onerror');
        }),
        onstop: $.Callbacks("unique").add(function() {
            //console.log('onstop');
        })
    };

    var methods = {

        construct: function(){
        },
        create: function(options) {

            var defaults = {
                width: 320,
                height: 240,
                background: '#000',
                onready: function(){},
                onstop: function(){}
            };

            var options = $.extend(defaults, options);

            return this.each(function(i, elements) {

                for (var prop in options) {
                    if (prop in callbacks) {
                        callbacks[prop].add(options.prop);
                    }
                }

                callbacks.onready.fire();

            });

        },
        stop: function() {

            return this.each(function(i, elements) {
                callbacks.onstop.fire();
            });
        }
    };

    $.fn.myplugin = function(option) {

        if (methods[option]) {
            return methods[option].apply( this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof option === 'object' || ! option) {
            return methods.construct.apply(this, arguments);
        } else {
            $.error('Method ' +  option + ' does not exist in jQuery.plugin');
        }    
    };

})(jQuery);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:35:53+00:00Added an answer on June 16, 2026 at 2:35 am

    I used a globalOptions variable to store the plugins initialization options as global,

    Here’s the two files to test things out and use as a stencyl.

    jquery.plugin.js
    (function($){

        var pluginName = 'myPlugin';
    
        var globalOptions = '';
    
        var methods = {
    
            create: function(options) {
    
                var defaults = {
                    backgroundColor: '#000',
                    color: '#fff',
                    oncreate: function(){},
                    onchangecolor: function(){},
                    onloadpage: function(page){}
                };
    
                globalOptions = $.extend(defaults, options);
    
                return $(this).each(function(i, elements) {
    
                    $(elements).data('globalOptions', globalOptions);
    
                    globalOptions.oncreate();
    
                });
    
            },
            changeColor: function(){
    
                return $(this).each(function(i, elements) {
    
                    globalOptions = $(elements).data('globalOptions');
                    if (!globalOptions) { $.error('Error: ' + pluginName + ' has not been initialized yet'); return false; }
    
                    $(this).css('background-color', globalOptions.backgroundColor);
                    $(this).css('color', globalOptions.color);
    
                    globalOptions.onchangecolor();
    
                });
            },
            loadPage: function(options){
    
                return $(this).each(function(i, elements) {
    
                    globalOptions = $(elements).data('globalOptions');
                    if (!globalOptions) { $.error('Error: ' + pluginName + ' has not been initialized yet'); return false; }
    
                    globalOptions.onloadpage(options.page);
                    location.href = options.page;
    
                });
            }
    
        };
    
        $.fn.myPlugin = function(option) {
    
            if (methods[option]) {
                return methods[option].apply(this, Array.prototype.slice.call(arguments, 1));
            } else if (typeof option === 'object' || ! option) {
                return methods.create.apply(this, arguments);
            } else {
                $.error('Error: Method "' + option + '" does not exist in ' + pluginName);
                return false;
            }    
        };
    
    })(jQuery); 
    

    index.html

    <!doctype html>
    <html>
        <head>
            <meta charset="utf-8" />
            <title>Example</title>
            <script src="jquery.min.js"></script>
            <script src="jquery.plugin.js"></script>
            <script>
    $(document).ready(function() {
    
        var myElement = $('.container').myPlugin({
            color: '#555',
            backgroundColor: '#ddd',
            oncreate: function(){
                console.log('Callback: create');
            },
            onchangecolor: function(){
                console.log('Callback: onchangecolor');
            },
            onloadpage: function(page){
                console.log('Callback: onloadpage = '+page);
            }
        });
    
        $('.color').click(function(event){
            myElement.myPlugin('changeColor');
        });
    
        $('.page').click(function(event){
            myElement.myPlugin('loadPage', { page: '#test' });
        });
    
    });
            </script>
        </head>
        <body>
            <div class="container">
                <button class="color">changeColor</button> <button class="page">loadPage</button>
            </div>
        </body>
    </html>
    

    If anyone has a better way of doing this and a way to add “private” methods, please share. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a button that has a jQuery click on the fly.
so I want to use this JQuery plugin that Stack Overflow has made available
I am trying to create a Jquery plugin that maintains chainability and has public
I'm looking for a jQuery plugin that can help me with the following: I
I have a website that has members. I want to create a feature that
I'm working on a small app that makes use of the jQuery plugin Fullcalendar.
Any online inplace editor recommend? I need some jquery plugin that can inplace edit
I am trying to create a plugin very that has similar usage to the
I am working with jQuery 1.7.2. I have create a custom plug-in that loads
I DO NOT WANT TO CREATE A JQUERY PLUGIN Let's say I have something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.