Making a copy of the DOM in jQuery

In a recent question we learned how to use cloneNode to make a copy of the document in JavaScript. Does it make sense to do something similar in jQuery? That is, something like

old$ = $.cloneNode(true);


if (old$('#myId').html() == 'Hello, world!') {...}

Upvotes: 0

Views: 30

Answers (1)

imvain2
imvain2

Reputation: 15847

You can use jquery's clone function to clone the body.

$(document).ready(function(){
  var _clone = $("body").clone();
  console.log(_clone.find("div").html());
});
<html>
<head>
</head>
<body>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>https://api.jquery.com/clone/</div>

</body>
</html>

Upvotes: 1

Related Questions