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 9064379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:14:09+00:00 2026-06-16T16:14:09+00:00

I have a jquery plugin that uses popular plugin pattern. The plugin opens a

  • 0

I have a jquery plugin that uses popular plugin pattern. The plugin opens a modal window and the modal window can also be closed. I also want to add some more functionality like, closing the window by pressing the escape key or closing the window by clicking outside the modal. How do I integrate this new code into the plugin?

NOTE: Also note that event handlers are attached to the body and document containers which is present outside the Modal window created using the plugin. I’m aware that in order to add stuff to the plugin I can attach methods to Plugin.prototype.xxx. Will I be able to do the same here or we should handle this case differently?

//press 'Esc' key - hide Modal
        $('body').bind('keydown', function(e) {
            if (e.keyCode == 27) { // "Esc" Key
                   if ( $('.show').is(':visible') ) {
                       $('.Close').click();
                   }  
                   return false;
            }
        });

        //click outside Modal - hide Modal
        $(document).mouseup(function (e){
            var container = $(".Window");
            if (!container.is(e.target) && container.has(e.target).length === 0){
                 $('.Close').click();
            }
        });

Popular plugin pattern I’m using for the plugin:

;(function ( $, window, document, undefined ) {
    // Create the defaults once
    var pluginName = 'defaultPluginName',
        defaults = {
            propertyName: "value"
        };

    // The actual plugin constructor
    function Plugin( element, options ) {
        this.element = element;

        this.options = $.extend( {}, defaults, options) ;

        this._defaults = defaults;
        this._name = pluginName;

        this.init();
    }

    Plugin.prototype.init = function () {

    };

    $.fn[pluginName] = function ( options ) {
        return this.each(function () {
            if (!$.data(this, 'plugin_' + pluginName)) {
                $.data(this, 'plugin_' + pluginName, 
                new Plugin( this, options ));
            }
        });
    }

})( jQuery, window, document );
  • 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-16T16:14:10+00:00Added an answer on June 16, 2026 at 4:14 pm

    Just add the code in the init method, but be shure you only target the desired elements.

    Plugin.prototype.init = function(){
        this.bindEventHandlers();
    }
    Plugin.prototype.bindEventHandlers = function(){
        var self = this;
    
        $('body').bind('keydown', function(e) {
            if (e.keyCode == 27) { // "Esc" Key
                   if ( $('.show', self.element).is(':visible') ) {
                       $('.Close', self.element).click();
                   }  
                   return false;
            }
        });
    
        //click outside Modal - hide Modal
        $(document).mouseup(function (e){
            var container = $(".Window", self.element);
            if (!container.is(e.target) && container.has(e.target).length === 0){
                 $('.Close', self.element).click();
            }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the data table from the jquery plugin dataTables (http://datatables.net/) that I want
I have a modal dialog plugin written in jquery, that binds to the click
I have a form that uses this plugin: http://bassistance.de/jquery-plugins/jquery-plugin-validation/ Works great - but I
In my local machine i have a scaffold that uses Jquery Validation plugin for
I have been working on a jquery plugin that uses a HTML5 audio player
I have a page that uses the jQuery.swfobject plugin to embed 5 flash items
I have a site that uses the jQuery jEditable plugin (inline editing via AJAX).
I have a jQuery plugin that uses a needle gauge. Upon initially loading a
I have developed a wordpress plugin that uses jquery. This plugin have some javascript
I have a Wordpress site that uses a JQuery plugin called Hover-Caption ( https://github.com/coryschires/hover-caption

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.