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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:13:24+00:00 2026-05-17T01:13:24+00:00

I have this interesting jQuery function. It basically adds a click handler to link,

  • 0

I have this interesting jQuery function. It basically adds a click handler to link, and when that is clicked, it will load a form to allow the user to edit the content. and the form is submitted by AJAX, and will display a success message when it’s done.

The outline is below; needless to say, this is messy. I could have each of the callback as a class method. What other ways are there to refactor nested functions? I am also interested to see if there are ways that variables declare in a parent function still retain its value down to the nested function after refactoring

$('a.edit').click( function() {

   // ..snipped..
   // get form
   $.ajax({
       success: function() {

         // add form
         // submit handler for form
         $(new_form).submit(function() {

            // submit via ajax
            $.ajax({

                success: function(data) {
                    // display message
                }
            })

         })

       }}
   )
}
  • 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-17T01:13:25+00:00Added an answer on May 17, 2026 at 1:13 am

    I guess the interesting part of your question is how to refactor without loosing access to the closure variables. Here is my suggestion:

    Version one: nested, with closures and variable access:

       var a;
        $('a.edit').click( function() {
          var b;
          $.ajax({
            success: function() {
              var c;
              $(new_form).submit(function() {
                var d;
                $.ajax({
                    success: function(data) {
                       // a,b,c,d are all visible here.
                       // note that a references the same object for all calls of the success function, whereas d is a different variable for each call of submit.
                       // this behaviour is called closure: the 'enclosed' function has access to the outer var
                    }
                })
              })
            }
          })
        })
    

    Version two: less nested, but without closures and without variable access:

    var a;
    $('a.edit').click(onEdit);
    
    var onEdit = function() {
      var b;
      $.ajax({success: onEditSuccess});
    };
    
    var onEditSuccess = function() {
      var c;
      $(new_form).submit(onSubmit);
    };
    
    var onSubmit = function() {
      var d;
      $.ajax({success: onSubmitSuccess});
    }
    
    var onSubmitSuccess = function(data) {
      // a is visible (global var)
      // b,c,d NOT visible here.
    };
    

    Version three: less nested and with unnamed functions and parameters to get access to the closure variables:

    var a;
    $('a.edit').click(function(){onEdit(a)});
    
    var onEdit = function(a) {
      var b;
      $.ajax({success: function(){onEditSuccess(a,b)}});
    };
    
    var onEditSuccess = function(a,b) {
      var c;
      $(new_form).submit(function(){onSubmit(a,b,c)});
    };
    
    var onSubmit = function(a,b,c) {
      var d;
      $.ajax({success: function(data){onSubmitSuccess(data,a,b,c,d)}});
    }
    
    var onSubmitSuccess = function(data,a,b,c,d) {
      // a,b,c,d are visible again
      // nice side effect: people not familiar with closures see that the vars are available as they are function parameters
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's an interesting problem: I have some jQuery that looks like this: $(document).ready(function() {
Here is my function: $(.link).click(function(event){ event.preventDefault(); event.stopPropagation(); pageLoad = $(this).data('page'); $('div#loading').fadeIn(); $('div#content').load('pages/'+pageLoad); }); I
Basically I am trying to have jQuery replace the button with the form and
This is an interesting problem that I have searched here and all over my
I have this interesting obstacle on my project. I am currently doing a MVC3
As part of my ecommerce application, I have this interesting problem, I've been trying
i have been reading this interesting article which is increasing my every growing confusion
Alright, this one's interesting. I have a solution, but I don't like it. The
I have a really interesting case: I have a site where this is already
I have a window with 3 radiobuttons like this(removed all non interesting props): <Control

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.