Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a jQuery set into HTML

Tags:

jquery

Given a jQuery result set, how do you convert that back into plain HTML?

<div class="abc">
    foo <strong>FOO</strong>
</div>
<div class="def">
    bar
</div>

--

var $mySet = $('div');

Given $mySet, how would you go about returning to the plain HTML above?

like image 491
nickf Avatar asked Nov 28 '08 05:11

nickf


People also ask

What is$() HTML?

jQuery | html() Method The html() Method in jQuery is used to set or return the innerHTML content of the selected element. Syntax: It returns the content of first matched element. $(selector).html() It sets the content of matched element.

How to get innerHTML of element 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.

How define jQuery in HTML?

Step 1: Firstly, we have to open that Html file in which we want to add the jQuery using CDN. Step 2: After then, we have to place the cursor between the head tag just before the title tag. And, then we have to use the <script> tag, which specify the src attribute for adding.

What is parseHTML in JavaScript?

parseHTML uses native methods to convert the string to a set of DOM nodes, which can then be inserted into the document. These methods do render all trailing or leading text (even if that's just whitespace).


1 Answers

i'd suggest creating a temporary container, then grabbing the html() of that new container:

var html = $('<div>').append( $('div').clone() ).html();
alert(html);

// alerts:

<div class="abc">
    foo <strong>FOO</strong>
</div><div class="def">
    bar
</div>
like image 168
Owen Avatar answered Sep 21 '22 04:09

Owen