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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:54:08+00:00 2026-06-11T10:54:08+00:00

I have the following code: function submitHandler($link, $modal, close) { var $form = $modal.find(‘#main-form’);

  • 0

I have the following code:

  function submitHandler($link, $modal, close) {

    var $form = $modal.find('#main-form');
    var $submitBt = $modal.find('.block-footer button:contains("Submit")');
    var oSubmit = {
        $form: $form,
        $modal: $modal,
        action: $form.attr('data-action'),
        entity: $form.attr('data-entity'),
        href:   $form.attr('data-href'),
        row: $link.attr('data-row'),
        $row: $('#row_' + $link.attr('data-row')),
        $submitBt: $submitBt
    };

    if (!$form.valid || $form.valid()) {
        $submitBt.disableBt();
        $modal.removeBlockMessages()
            .blockMessage('Contacting Server, please wait ... ', { type: 'loading' });
        $.ajax({
            url: oSubmit.href,
            dataType: 'json',
            type: 'POST',
            data: $form.serializeArray()
        })
        .done(function (json, textStatus, XMLHttpRequest) {
            json = json || {};
            if (json.success) {
                submitSuccessModal(oSubmit, json);
                if (close == true) {
                    $modal.closeModal();
                    if (oSubmit.action == "Create") {
                        $('#createLink').prop('disabled', false);
                    }
                }
            } else {
                submitFailModal(oSubmit, json);
            }
        })
        .fail(function (XMLHttpRequest, textStatus, errorThrown) {
            setTimeout(function () {
                $modal
                    .removeBlockMessages()
                    .blockMessage('Could not contact server, please try again', { type: 'error' });
                $submitBt.enableBt();
            }, 1000);
            return false;
        });
    };
};

What’s the best way to move the functionality out of my $.ajax and out into a function for “done” and another function for “fail”. Also what parameters should I be passing to these functions? I guess what I am most confused about is what should I do about the variables I have declared at the top of my code. Should I pass these also to the external functions as in the .done I do reference some of them.

  • 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-11T10:54:09+00:00Added an answer on June 11, 2026 at 10:54 am

    What you have to now makes use of what is known as anonymous functions. You pass a reference to an anonymous function (a function without a name) into the done and fail functions.

    Perhaps to better organize your code (or to make these functions shareable with other code), all you need to do is give these functions a name and move them outside the ajax() call. We’ll call them onDone and onFail, though you can of course call them whatever makes sense.

    Notice I reference them without parathesis, since I’m referring to the function object itself, and not the return value of the called function.

      function onDone(json, textStatus, XMLHttpRequest) {
           json = json || {};
           if (json.success) {
               submitSuccessModal(oSubmit, json);
               if (close == true) {
                   $modal.closeModal();
                   if (oSubmit.action == "Create") {
                       $('#createLink').prop('disabled', false);
                   }
               }
           } else {
               submitFailModal(oSubmit, json);
           }
       }
    
       function onFail(XMLHttpRequest, textStatus, errorThrown) {
           setTimeout(function () {
               $modal
                   .removeBlockMessages()
                   .blockMessage('Could not contact server, please try again', { type: 'error' });
               $submitBt.enableBt();
           }, 1000);
           return false;
       }
    
       $.ajax({
           url: oSubmit.href,
           dataType: 'json',
           type: 'POST',
           data: $form.serializeArray()
       })
       .done(onDone) //Reference to onDone, without parenthesis
       .fail(onFail);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: var submitHandler = function ($link, $form, close) { var
I have the following code: var btns1 = { 'Submit': function (win) { submitHandler(oLink.$link,
I have the following code: function show(){ var a=document.getElementById('somediv').style.display; a=block; } The above code
I have the following code: function reload() { var referenceID = $('#ReferenceID').val(); $('#detailData').load(Url.Action(DetailData, new
I have the following code: function showAddrBox() { var prompt = document.getElementById('addr_prompt'); prompt.style.display =
I have the following code: function header(){ experience += ''; var expimage = '';
I have the following code: function callIframe(type, cat) { var send = false; var
I have the following code: function removeUsers() { var removedUsers = document.getElementById('<%=removedUsers.ClientID%>'); var lbCurrent
I have the following code: (function ($) { /** * Opens a new modal
If I have the following code: function foo() { var a = []; for(var

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.