For example, if the match is <div class="class1">Hello world</div>
, I need to return
<div class="class1">Hello world</div>
not just "Hello world".
Thanks!
text() – This method sets or returns the text content of elements selected. html() – This method sets or returns the content of elements selected.
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. When this method is used to set content, it overwrites the content of ALL matched elements.
append( function ) A function that returns an HTML string, DOM element(s), text node(s), or jQuery object to insert at the end of each element in the set of matched elements.
What is the use of html() method in jQuery ? The html() method in jQuery is used to get the contents of the first element in the set of matched elements or is used to set the HTML contents of every matched element. It returns the content of the first matched element. This function does not accept any arguments.
There's no built-in function for getting the outerHTML, but you can use this:
jQuery.fn.outerHTML = function(s) {
return (s)
? this.before(s).remove()
: jQuery("<p>").append(this.eq(0).clone()).html();
}
Then in your selector:$('.class1').outerHTML()
will give you what you are looking for.
Source of function
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