<div>
<a href="#" class="selected">link1</a>
<a href="#">link1</a>
</div>
and using following
$('.selected').html()
I get
link1
as a return value.
How can I get full html code of the selected DOM element, in this example to get
<a href="#" class="selected">link1</a>
instead?
thanks
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.
jQuery clone() Method The clone() method makes a copy of selected elements, including child nodes, text and attributes.
To get HTML content of an element using jQuery, use the html() method. The html() method gets the html contents of the first matched element.
We can replace HTML elements using the jQuery . replaceWith() method. With the jQuery replaceWith() method, we can replace each element in the set of matched elements with the provided new content and return the set of elements that were removed.
jQuery object:
$('.selected')
become to DOM object:
$('.selected')[0]
UPDATE:
var str = $("<div />").append($('.selected').clone()).html();
console.log(str);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With