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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:20:29+00:00 2026-06-14T03:20:29+00:00

I would like to reset the DOM to original state and then manipulate it

  • 0

I would like to reset the DOM to original state and then manipulate it again.

I have found that the best way to do this is as follows:

// Clone the Dom and assign it to a variable
divClone = $("#document").clone();

// .... More Code ......

// When required, replace the DOM with with the cloned variable. 
       $("#document").replaceWith(divClone);   

The only problem is that you cannot manipulate the newly reseted DOM again.

I have put together a JSFiddle which uses a simple example of adding a class.
You can click on the “test” link to add a a class. You can then click on “Reset” to return the DOM back to its original state. However, if you click on the “test” link again, no more classes will be added to the restored DOM.
(Obviously, this is just a simplified example. There are better ways of removing and adding classes).

How can I manipulate the restored DOM?

  • 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-14T03:20:30+00:00Added an answer on June 14, 2026 at 3:20 am

    Your code works as expected, the problem is the DOM you cloned does not have handlers bound to it.

    You have 2 options, first one is to clone after all the handlers are bound, passing true to the withDataAndEvents clone method argument:

    $(".test").click(function() {
        $("#document").addClass("green");
    });
    
    $(".reset").click(function() {
        $("#document").replaceWith(divClone.clone(true));
    });
    
    var divClone = $("#document").clone(true);
    

    Fiddle


    The 2nd option is to use event delegation.

    var divClone = $("#document").clone();
    
    $(document).on('click', '.test', function() {
        $("#document").addClass("green");
    }).on('click', '.reset', function() {
        $("#document").replaceWith(divClone.clone());
    });
    

    Fiddle

    The drawback to this second approach is that all events have to bubble up to the document level and you can’t cancel the bubbling with event.stopPropagation() then.


    Edit: Updated answer to replace with a clone of the clone, so that the original clone always stays safe at initial state and can serve as base for later resets.

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

Sidebar

Related Questions

Hello, I have a short question. I would like to reset a variable that
I have a problem with my app. I would like to 'reset' my app
I would like to completely reset my %hash so that it does not contain
I would like for it display all lines that have all the values that
I would like to completely reset the scroll position of a UITableView, so that
I have around 10 JUnit that will be executed automatically. I would like to
I would also like to reset the font-size of <small> tag too normal HTML
I would like for my app to reset to standard settings if it crashed
Would like a for loop in jquery so that: For every hover_link: show hidden
I would like to reset passwords on a bunch of boxes over SSH. Any

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.