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

  • Home
  • SEARCH
  • 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 9132011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:14:21+00:00 2026-06-17T08:14:21+00:00

When I try to build up a sequence of disconnected DOM nodes using .after

  • 0

When I try to build up a sequence of disconnected DOM nodes using .after, it works fine if they are empty:

[14:56:45.186] $('<span></span>').after('<p></p>');
[14:56:45.193] ({0:({}), length:2, prevObject:{0:({}), length:1}, context:(void 0), selector:".after([object Arguments])", 1:({})})

But if I try to add any text in the first node, it fails:

[14:56:41.521] $('<span>test</span>').after('<p></p>');
[14:56:41.525] ({0:({}), length:1})

If I assign that result to a variable and try to inspect it, it appears as if after had never been called at all.

What’s going on here, and how do I work around it?


Edit: For those interested, I ended up writing the following wrappers which seem to be making life much easier for me:

function tag(name) {
    return function(contents, options) {
        var o = options || {};
        var is_array = $.type(contents) === "array";
        if (!is_array) {
            o.text = contents;
        }
        result = $('<' + name + ' />', o);
        if (is_array) {
            $.each(contents, function(i, child) { result.append(child); });
        }
        return result;
    }
}
var span = tag('span');
var div = tag('div');
  • 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-17T08:14:22+00:00Added an answer on June 17, 2026 at 8:14 am

    To clarify your bug — when I run your code in my JavaScript console, I get the following:

    > $('<span></span>').after('<p></p>');
    [<span>​</span>​,<p>​</p>]
    > $('<span>test</span>').after('<p></p>');
    [<span>​test​</span>​]
    

    According to the jQuery 1.9 upgrade guide:

    Prior to 1.9, .after(), .before(), and .replaceWith() would attempt to
    add or change nodes in the current jQuery set if the first node in the
    set was not connected to a document, and in those cases return a new
    jQuery set rather than the original set. This created several
    inconsistencies and outright bugs–the method might or might not
    return a new result depending on its arguments!
    As of 1.9, these
    methods always return the original unmodified set and attempting to
    use .after(), .before(), or .replaceWith() on a node without a parent
    has no effect–that is, neither the set or the nodes it contains are
    changed.

    So: The behavior you’ve observed is a bug which results from using .after() on a node without a parent. As of 1.9, the jQuery dev team has solved this inconsistency by removing it entirely — using .after() this way should always return the initial node without the <p> after it (fiddle).

    Workaround:

    Push your DOM nodes onto a simple array. Or append them to a parent node, then get all the children: (fiddle)

    $chain = $('<div>').append('<span></span>').append('<p>qwer</p>').children();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to build a JSF 2.0 Web application which is using libraries including
I try to build a menu using the datatype hierarchyid . I have the
I try to build chart demo using Qwt and C++. I've added the following
I try to build a soccer manager game using rails and I'm stuck with
I try build wrap panel with vertical buttons. Every button consist from image and
I try to build a nice box with only 1 image and 1 div.
I try to build a metro-style-app with Javascript and when I run the Windows
i try to build a app that is in landsscape mode all the time.
Im try to build my plugins that sit in a seperate directory on the
I try to build libtorrent(rasterbar- libtorrent). It cannot be build. the build log is

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.