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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:23:26+00:00 2026-05-27T14:23:26+00:00

replaceWith() is not working properly for me. For example, given this HTML: <div id=tree1>

  • 0

replaceWith() is not working properly for me. For example, given this HTML:

<div id="tree1">
    <div>
        <h3>Item 1</h3>
        <h3>Item 2</h3>
        <h3>Item 3</h3>
    </div>
</div>

this code does not replace the <h3> nodes with <li>s:

var items_Bad   = $("#tree1 div h3").clone ();

$("#tree1 div").remove ();
$("#tree1").append ('<ul id="Items1"></ul>');

//--- This does not replace anything!
items_Bad.replaceWith (function () {
    return ('<li>' + $(this).text () + '</li>');
} );

$("#Items1").append (items_Bad);

But this code does! :

var items_Good  = $("#tree1 div h3").clone ();

$("#tree1 div").remove ();
$("#tree1").append ('<ul id="Items1"></ul>');

$("#Items1").append (items_Good);

//--- But, this works!
$("#Items1 h3"). replaceWith (function () {
    return ('<li>' + $(this).text () + '</li>');
} );

See the jsFiddle.

Here, replaceWith() works only if the nodes are (re)attached to the DOM, but the documentation says that:

As of jQuery 1.4, .replaceWith() can also work on disconnected DOM
nodes.

What am I missing?

(Note that I don’t control the original page’s source. And, I have a lot more manipulation to do, so reattaching the nodes prematurely to manipulate them is undesirable.)

  • 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-27T14:23:27+00:00Added an answer on May 27, 2026 at 2:23 pm

    The code below is the part of replaceWith() that deals with replacing detached nodes (taken straight from the jQuery 1.7.1 source, modulo minor formatting differences):

    replaceWith: function(value) {
        if (this[0] && this[0].parentNode) {
            // This deals with attached nodes...
        } else {
            return this.length
                ? this.pushStack(jQuery(jQuery.isFunction(value) ? value() : value),
                    "replaceWith", value)
                : this;
        }
    }
    

    As you can see, there are two limitations that prevent this code from fulfilling your requirements:

    • The supplied function is only called once instead of once per element in the set,
    • The supplied function is called in the global context (i.e. the element being replaced is not available through the this keyword).

    So, I’m afraid replacing detached nodes is still a work in progress, and you will have to fall back to your second solution until the limitations above are fixed.

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

Sidebar

Related Questions

Why is this not working? $(document).ready(function(){ var content = '<a href=http://example.com/index.php><b>Some text!</b></a> - <a
I'm trying to use .replaceWith to replace words to a function but it's not
Here's my html: <div class=timer>Not Started</div> And JS/JQ: var seconds = 10; var minutes
I'm working on this pretty big re-factoring project and I'm using intellij's find/replace with
My site uses jQuery 1.4.2. The problem is .replaceWith() does not work in IE6
I used to use this script for jquery email obfuscation: $(.replaceAt).replaceWith(@); $(.obfuscate).each(function () {
Say I have a query like this: one two three, if I replace with
As asked and answered in this post , I need to replace '[' with
Disclaimer: I am unable to implement this properly in the application, as the application
I have the following HTML file: <div class=tituloD> </div> <div class=subtituloD> </div> <div class=puntosD>

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.