Adam Pietrasiak
Adam Pietrasiak

Reputation: 13194

JS UNDO DOM modification

Is it possible to undo last DOM modification?

I've tried to remember state after any change by

undoArray = [ $("#container").clone() ];

//some situation when dom changed and remember state
undoArray.push( $('#container').clone() );

And then, if needed, to restore #container content from this array, but it was loosing all events attached directly to elements.

Upvotes: 2

Views: 289

Answers (2)

user2736012
user2736012

Reputation: 3543

Pass true to .clone() to keep events and data.

undoArray = [ $("#container").clone(true) ];

Docs for .clone()

Just remember to clean the data if you're going to destroy an item in the Array without putting it in the DOM.

Upvotes: 3

Murali Murugesan
Murali Murugesan

Reputation: 22619

If you want to persist the events then pass true for clone() withDataAndEvents

  $("#container").clone(true)

or attach using on() to the document

  $(document).on('eventName','targetSelector', eventHandler);

Upvotes: 1

Related Questions