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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:16:21+00:00 2026-05-16T01:16:21+00:00

Within document.ready, I store a selector in a variable (which I assume once evaluated,

  • 0

Within document.ready, I store a selector in a variable (which I assume once evaluated, stores the element object in said variable), as I need to manipulate it many times.

ie. myVar = $('#mySpan');

What I’m storing is a span element within a div. One of the manipulations requires me to overwrite the contents of the div using the .html() function.

After, I need to reinsert that span element inside the div mentioned above and do so using the html() function.

The problem I have is that once the span element has been re-added, the variable myVar is no longer associated with the span element. I assume this is because it was deleted from the DOM and then re-added.

I know that jquery has the .detach() function, but that seems to only preserve attached events and data elements, but not actual references. I don’t even thing the .live() function would help in this case either.

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

    EDIT:

    If you want to preserve the content in a variable for later insertion, just simply store it in a variable using .contents(), then reinsert it when needed.

    jQuery’s .contents() method selects all content, including text nodes.

    • http://api.jquery.com/contents/

    Try it out: http://jsfiddle.net/bD2Ej/2/ (click the text to toggle)

    This example toggles between the original and new content.

    var myVar;
    
    // Add the new content, and append the element stored in myVar
    $('#somediv').toggle(function() {
        myVar = $(this).contents();
        $(this).html('some new content<br>');
    }, function() {
        $(this).html( myVar );
    });
    ​
    

    Original ansewr:

    If you used .detach() you would still need to store it in a variable.

    Here’s an example: http://jsfiddle.net/bD2Ej/

       // Detach and reference the element in a variable
    myVar = $('#mySpan').detach();
    
       // Add the new content, and append the element stored in myVar
    $('#somediv').html('somecontent').append( myVar );
    

    But are you sure you need .html()? Could you just use .append() so the #mySpan isn’t overwritten in the first place?

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

Sidebar

Ask A Question

Stats

  • Questions 471k
  • Answers 472k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I've found that HttpClient is like 9 times slower than… May 16, 2026 at 3:29 am
  • Editorial Team
    Editorial Team added an answer Not a conclusive answer, but it looks like it was… May 16, 2026 at 3:29 am
  • Editorial Team
    Editorial Team added an answer A web application needs to be compiled before it works;… May 16, 2026 at 3:29 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.