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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:35:51+00:00 2026-05-23T11:35:51+00:00

I have a global variable var $test = $(‘#test’); where #test id of div

  • 0

I have a global variable var $test = $('#test'); where #test id of div that initially exist in DOM. If i remove the div with remove() and append it again then the global variable $test no longer recognizes the the newly appended div with id #test. What am i doing wrong. Why does this happen?

Check http://jsfiddle.net/mr3C4/

  • 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-23T11:35:51+00:00Added an answer on May 23, 2026 at 11:35 am

    Instead of creating a new element from the string, just append the original element:

    var $test = $('#test');
    $test.live('click', function() {
        $test.toggleClass('blue');  // 2. continues to reference $test
    });
    
    $('button').click(function() {
        $test.remove();
        $test.appendTo('body');  // <-- 1. append the same element that you removed
    });
    

    Example: http://jsfiddle.net/mr3C4/5/

    That way your handler is still referencing the same element you have referenced in the $test variable.

    This is because $test is referencing a specific jQuery object that references a specific element, which is no longer part of the DOM. You were recreating an idential element from a string in your original code.


    …or you can use the detach()[docs] method instead of the remove()[docs] method if you want to keep jQuery data associated with it.

    var $test = $('#test');
    $test.live('click', function() {
        $test.toggleClass('blue');
    });
    
    $('button').click(function() {
        $test.detach();
        $test.appendTo('body');
    });
    

    Example: http://jsfiddle.net/mr3C4/6/


    EDIT:

    If you want to update your element, it is better to use the method above, and make modifications to the original.

    If you can’t do that because you’re making a change that can’t be made to the original, like changing the tagName, then create your new element, and update your variable.

    var $test = $('#test'),
        x = '<p id="test"></p>';
    $test.live('click', function() {
        $test.toggleClass('blue');
    });
    
    $('button').click(function() {
        $test.detach();
        $test = $( x ).appendTo('body'); // update the variable
    });
    

    You could even keep the same jQuery object, and just update the element:

    var $test = $('#test'),
        x = '<p id="test"></p>';
    $test.live('click', function() {
        $test.toggleClass('blue');
    });
    
    $('button').click(function() {
        $test.detach();
        $test[0] = $( x ).appendTo('body')[0]; // update the element at index 0
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If i have global variable in A.dll, that depends on global variable in B.dll
I have a global variable that is an instance of my custom class. How
I have a vector declared as a global variable that I need to be
I have for example the following URL stored in a global variable: var myUrl
For example, I have $theme global variable that is defined in the configuration file,
So in jQuery, I have a global variable currentSubNav that stores a current visible
I seem to have an array scope issue. I have a global variable; var
I'd like to assign part of the DOM tree to global variable, so that
I have global variable fan_coil_ai_bi, and I use that variable to store some data
I have this code var myObjects = {}; //global variable //Later on in the

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.