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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:51:01+00:00 2026-05-26T01:51:01+00:00

…if the last command doesnt have a callback function? Like for example $(something).remove(other_thing).append(new_thing) Append

  • 0

…if the last command doesnt have a callback function? Like for example

$(something).remove(other_thing).append(new_thing)

Append doesnt have a callback function, so my question is, is there a jQuery method (or other way) to call a passed function after a chain of commands? The function doesnt have to affect the current selection.

EDIT: I know the methods in this example shouldnt take long so a callback wouldnt be necessary, but that was just the first example I could think of. so please think of it as if this wasnt the case

  • 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-26T01:51:02+00:00Added an answer on May 26, 2026 at 1:51 am

    To expand on Craig’s answer, you simply invoke your function normally in the next statement.

    The purpose of a callback function is so that you can do something after something else—which will take an indeterminate amount of time, like a network (AJAX) call—completes. Key to this is that the other operation does not block. In other words, it happens—or, from your script’s perspective, appears to happen—on another thread.

    Consider how an AJAX call works.

    var d;
    $.get('/something', function(r) {
        // this is a callback function
        d = r;
        alert(d);
    });
    // this is the next statement
    console.log(d);
    

    The call to $.get executes first in this program. The first parameter is the URL you want to fetch; the second is an anonymous callback. It will be executed after the browser connects to the server, requests /something, and eventually receives a response. These things happen in the background while your script continues to execute.

    The call to console.log executes after the call to $.get returns. Since $.get returns immediately, the log call happens before the browser has even had a chance to connect to the server. This means d will be undefined, and your call to console.log will just show “undefined”.

    This is where the usefulness of a callback comes in to play. When the background request completes, the browser fires an event on the XHR object that jQuery used to issue your request. jQuery then calls your callback function with the result of the request. In this function, we now have the data, can set d, and the call to alert has your data. Magic!

    Now consider how jQuery’s DOM manipulation functions (like append) work. They are synchronous. That means that your call to append blocks—and your script can do nothing else—until the function has completed all of its work.

    This is a consequence of how the browser’s native DOM manipulation functions work. They don’t return until the change to the DOM is complete.

    Thus, it is not useful for jQuery to provide callback functions on DOM manipulation methods (or any other synchronous method).

    Here’s what append looks like from a vastly simplified standpoint:

    $.fn.append = function(elem) {
        // do stuff, specifically
        this.appendChild(elem);
    };
    

    Here’s what it would look like if we added the option to have a callback:

    $.fn.append = function(elem, callback) {
        // do stuff, specifically
        this.appendChild(elem);
        if (callback) callback();
    };
    

    And now, what you would do from your code, respectively:

    $('#something').append('<b>Foo</b>');
    addBar();
    console.log('Done');
    

    ​

    $('#something').append('<b>Foo</b>', function() {
        addBar();
    });
    console.log('Done');
    

    These two snippets are guaranteed to execute exactly the same, every time.

    1. You call .append. jQuery works some if its magic, and eventually calls the native appendChild on #something.
    2. appendChild appends the new node(s) to #something. The DOM is modified, and internal data structures are updated to reflect the changes. If you are appending many nodes, this might take a while.
    3. Control returns from appendChild.
    4. What happens next depends on which ‘version’ we’re talking about.
      • In the normal version:
        1. Control returns from .append().
        2. The next statement of your script executes. In this case, we call addBar().
        3. When addBar is done, the next statement, console.log('Done') is called.
      • In the callback version:
        1. .append() checks if you’ve supplied a callback, and you have, so…
        2. .append() calls the callback function.
        3. In the callback, we call addBar().
        4. When addBar is done, control returns to the callback function, and since there’s nothing else in the function, it returns as well.
        5. Control has now returned to .append(), and there is nothing left to execute in that function, so control returns from .append().
        6. The next statement of your script executes. console.log('Done') is called.

    As you can see, no matter which way we do it,

    1. The element is appended to the DOM.
    2. addBar() is called.
    3. console.log() is called.

    As a rule: If a jQuery function does not provide the option to pass a callback, the function is synchronous, and therefore you can just proceed with your next step on the next line.


    Edit: If you want to call a function in the middle of a chain, jQuery doesn’t provide anything built-in, but we can write our own plugin!

    jQuery.fn.invoke = function() {
        var args = Array.prototype.slice.call(arguments), fn = args.shift();
        fn.apply(this, args);
        return this;
    };
    

    This takes the function you want to call as the first argument, then passes the rest of the arguments to that function. Within the function, this refers to the jQuery object from your chain. The function’s return value is ignored. Demo.

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

Sidebar

Related Questions

I have an element like this: <span class=tool_tip title=The full title>The ful&#8230;</span> This seems
CGI.escapeHTML is pretty bad, but CGI.unescapeHTML is completely borked. For example: require 'cgi' CGI.unescapeHTML('&#8230;')
I see that some rss on xml have strange strings. For example, ... is
I have been making a wordpress template. i got stuck at some place... the
An Arabic name shall be sent via SOAP. The name is encoded like this:
I have a form, one of the fields is a select field, with 5
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a MySQL query: SELECT px.player, px.pos, px.year, px.age, px.gp, px.goals, px.assists ,
what about this one: I want to format the currentTime displayed by a videoPlayer
English is not my native language. I need a software to spellcheck and correct

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.