Ideally you'd want to work with the HTML instead of manipulating it with JavaScript or jQuery.
The reasons being:
- JavaScript is going to take time to process the script.
- Having additional JS code, which isn't adding any value will lead to a maintenance nightmare down the road.
jQuery is a library, which gives us short-cuts for working with native/vanilla JavaScript. Here is what each of those 2 functions are doing.
.clone()
Create a deep copy of the set of matched elements.
.append()
Insert content, specified by the parameter, to the end of each element in the set of matched elements.
$("#div1") is the same as JavaScript's document.getElementById() method / function.
From the Mozilla Developer Network:
Returns a reference to the element by its ID; the ID is a string which can be used to uniquely identify the element, found in the HTML id attribute.
If you don't need to make jQuery chains...
var html = $("<div />").append($("#div1").clone()).html();
... you'll look like a smarter web developer to other web developers who may code review your work, if you work with the HTML like this...
<div id="div1"></div>
<div id="div2" class="blink"></div>
... and jQuery, like this:
let obj1 = $("#div1");
let obj2 = $("#div2");
// Do something with obj1 & obj2 or the HTML for both DOM nodes.
obj1.html('Hello');
obj2.html('World');
obj1.addClass('red');
obj2.removeClass('blink');
Then you should get:
<div id="div1" class="red">Hello</div>
<div id="div2">World</div>
Tip: Don't write extraneous code simply to obfuscate it. You'll have a hard time reading it later on. Smaller code is good code! It looks professional.
Keep on learning!