w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How to reset DOM state to the initial state (when page was firstly received?)

Because how JavaScript works (objects are not copied but passed by reference) initial_state var has just stored the reference to the DOM document. When the DOM document is updated your initial_state var will reflect those changes because it's just a reference which is just pointing at the DOM document, and not an object "on its own".

To obtain a copy of an object which is a "real" object and not just a reference to the first object, you need to clone.

If you use jQuery you can clone by using extend function

// for simple cloning
var initDoc = $.extend({}, document);

// for deep cloning
var initDocument = $.extend(true, {}, document);

If you are not interested in using jQuery.extend() please take a look this related question on cloning object in JavaScript.

Most elegant way to clone a JavaScript object





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.