Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In jQuery, are there any function that similar to html() or text() but return the whole content of matched component?

Tags:

jquery

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!

like image 336
jeffu Avatar asked Jun 15 '09 12:06

jeffu


People also ask

What is the difference between text () and HTML () methods in jQuery for get and set HTML elements?

text() – This method sets or returns the text content of elements selected. html() – This method sets or returns the content of elements selected.

What is the use of HTML () method in jQuery?

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.

Which jQuery function adds HTML content outside a selection?

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.

How HTML elements are used in jQuery with example?

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.


1 Answers

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

like image 159
Jose Basilio Avatar answered Sep 25 '22 18:09

Jose Basilio