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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:54:42+00:00 2026-05-28T13:54:42+00:00

I am attempting to create a jQuery plugin for my own use that can

  • 0

I am attempting to create a jQuery plugin for my own use that can be used to setup a handler for jQuery mobile dialogs using a single command as such: $('#dialog').setup_dialog({ callback: callback_function });

However, my handler has a rather obvious memory leak due to closures in it:

$.fn.setup_dialog = function( options ) {
    var settings = $.extend({
        callback : 0
    }, options);
    var that = this;

    return this.live('pagebeforeshow', function(event, ui) {
        console.log("outside");
        $('form', that).submit( function(e) {
            var $inputs = $('form :input', that); // get all form inputs

            var values = {};
            $inputs.each(function() {
                values[this.name] = $(this).val();
            });

            that.dialog('close');

            if ( settings.callback && typeof(settings.callback) === "function" ) {
                $('#'+ui.prevPage[0].id).live('pagebeforeshow', function() {
                    settings.callback(values, that);
                    console.log("inside");
                });
            }

            return e.preventDefault();
        });
    });
}; /* setup_dialog */

If you ran the above code, you would see “inside” and “outside” printed first once, then three times (twice from a single submit), then six times (three times from a single submit), etc.

The intent of the code is to attach an event handler to the jQuery Mobile dialog when it comes up that will catch form submission, collect all the form values, and then pass them to a callback function that will modify the original page (that launched the dialog).

Note that because of the way jQuery Mobile uses AJAX to switch between pages, I need to use .live to bind the events (.bind or .one won’t work).

Any ideas how I can avoid the events accumulating (and maybe clean up the code a bit as well)?

  • 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-05-28T13:54:43+00:00Added an answer on May 28, 2026 at 1:54 pm

    The solution appears to be the following:

    $.fn.setup_dialog = function( options ) {
        var settings = $.extend({
            callback : 0
        }, options);
        var that = this; var values = {}; var submitted = 0;
    
        this.on('pageinit', function(event, ui) {
            $('form', that).submit(function(e) {
                var $inputs = $('form :input', that); // get all form inputs
                submitted = 1;
    
                $inputs.each(function() {
                    values[this.name] = $(this).val();
                });
    
                that.dialog('close');
    
                return e.preventDefault();
            });
    
            $('.cancel-button', that).click(function() {
                submitted = 0;
            });
        });
    
        this.on('pagebeforehide', function(event, ui) {
            if ( submitted && settings.callback && typeof(settings.callback) === "function" ) {
                settings.callback(values, that);
            }
        });
    }; /* setup_dialog */
    

    There were multiple ways to do this, including creating a closure and calling $('#'+ui.prevPage[0].id).die().live('pagebeforeshow', function() {

    Not convinced this is the nicest solution though, so I’d love to hear some better ideas.

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

Sidebar

Related Questions

I'm attempting to use JQuery to create a dynamic page header using an image
I'm new to jQuery, but just attempting to create a function I can use
I'm attempting to create a postcode lookup JQuery script that'll be able to be
I'm attempting to create divs in an table using jQuery. The table represents a
I'm attempting to create a custom calendar control that inherits from ASP.Net's built in
I am attempting to create my own custom Autocomplete style dropdown control in c#
I am attempting to create a web page that will allow a user to
I am attempting to write some jQuery code that will expand a paragraph when
Attempting to create an itemgroup for use in a target where the file types
I'm attempting to create an array of strings that represent the directories stored in

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.