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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:48:51+00:00 2026-05-25T14:48:51+00:00

I am working through the jQuery documentation and found a piece of a replaceWith()

  • 0

I am working through the jQuery documentation and found a piece of a replaceWith() example (the last example from http://api.jquery.com/replaceWith/) that I don’t fully understand. I will post a link to this post as a comment on the jQuery documentation replaceWith() page to aid others new to jQuery and DOM manipulation.

Specifically, I do not fully understand the behavior of “$container” in:

"$("p").append( $container.attr("class") );"

I was expecting the above code to append the word “inner” to the “p” contents because the “replaceWith()” method was called when the variable was created:

var $container = $("div.container").replaceWith(function() {
    return $(this).contents();
});

However, “$(“p”).append( $container.attr(“class”) );” actually appends the word “container”, not the word “inner”.

It seems that the variable “$container” actually references the div that was replaced, “$(“div.container”)”, not the replacing content, “$(this).contents();”.

Two questions:

  1. What is “replaceWith()” doing in this context?
  2. Is there something special going on with the order of operations or the “attr()” method that I am not seeing?

Here is the full code:

<!DOCTYPE html>
<html>
<head>
    <style> 
    .container { background-color: #991; }
    .inner { color: #911; }
    </style>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

<p>
    <button>Replace!</button>
</p>
<div class="container">
    <div class="inner">Scooby</div>
    <div class="inner">Dooby</div>
    <div class="inner">Doo</div>
</div>

<script>
$('button').bind("click", function() {
    **var $container = $("div.container").replaceWith(function() {
        return $(this).contents();
    });**

    **$("p").append( $container.attr("class") );**
});
</script>

</body>
</html>
  • 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-25T14:48:51+00:00Added an answer on May 25, 2026 at 2:48 pm

    From the .replaceWith() documentation:

    The .replaceWith() method, like most jQuery methods, returns the jQuery object so that other methods can be chained onto it. However, it must be noted that the original jQuery object is returned. This object refers to the element that has been removed from the DOM, not the new element that has replaced it.

    …so what you’re seeing is the expected behavior, $container should and does refer to what was replaced, not the replacement.

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

Sidebar

Related Questions

Still working through JQuery to get data and repopulate portions of a page. Right
I am currently working through this tutorial: Getting Started with jQuery For the two
In working through the issues with another question, I've found text files with embedded
I'm currently working through an assignment that deals with Big-O and running times. I
I would like to add a class(ex) on landing or similarly through jQuery. from
I've read through the documentation for Jquery UI Tabs (1.7) and I believe I'm
I'm working through a jQuery image slider tutorial and I lost them at the
I'm working through a jQuery book and am currently on the chapter of table
I'm working through the Agile Toolkit DVD rental example. In the Day3 - Creating
I am trying to remove <br /> tag from wordpress native gallery through jquery

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.