Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery make a copy of HTML and store it for later retrieval

Tags:

jquery

clone

I need to be capable to restore the HTML code inside a div as it was at page ready. I need this because I want to make some changes to the HTML code after page ready and then revert it to the way it was at page ready when I need it..

I was thinking to use clone, but how do I just copy the content of the div without paste it? I need to copy the content of the div at page ready and then paste it/replace the div with the original when I need..

I am rather new to jquery, I tried several things without luck, I can't find out what I need. Thank you for any suggestion!

like image 581
Adrian M. Avatar asked Jul 12 '13 20:07

Adrian M.


People also ask

How copy HTML using jQuery?

To clone an element using jQuery, use the jQuery. clone() method. The clone() method clones matched DOM Elements and select the clones. This is useful for moving copies of the elements to another location in the DOM.

How do you clone HTML in Javascript?

First, select the <ul> with the id menu by using the querySelector() method. Second, create a deep clone of the <ul> element using the cloneNode() method. Third, change the id of the cloned element to avoid duplicate. Finally, append the cloned element to the child nodes of the document.

What is the jQuery method for retrieving the HTML content of an element?

jQuery html() Method The html() method sets or returns the content (innerHTML) of the selected elements. When this method is used to return content, it returns the content of the FIRST matched element.

What is cloning in jQuery?

jQuery clone() Method The clone() method makes a copy of selected elements, including child nodes, text and attributes.


1 Answers

You can copy it into a variable for later.

var originalContent;

$(document).ready(function() {
    originalContent = $('#theDiv').clone();
});
like image 180
Jason P Avatar answered Nov 09 '22 14:11

Jason P