Get inner html of div stored in var

html, javascript, jquery

Solution

You can pass HTML into the JQuery function and it will create an element in memory.

You can then manipulate that element like any other element that physically exists in your page.

$(function () {
    var rawString = "<div id='something' myAttr='somethingElse' > Free me!</div>";
    var cleanString = $(rawString).html(); // ... or .text()
    $('#content').append(cleanString);
});

Problem

I'm getting some data from a server, and it's coming down prepackaged in elements. The issue is that there is a bunch of stuff in those tags that I don't want. Does anyone have any suggestions to get only the html of those elements? I have something that works, but it's hack-y and worse yet, it assumes that I know something about the attributes of the tags, and I won't always: Here's some JQuery (see it in action here: http://jsfiddle.net/tPJau/): ``` $(function () { var rawString = "<div id='something' myAttr='somethingElse' > Free me!</div>"; $('#content').append(rawString); var cleanString = $('#something').html(); $('#something').remove(); $('#content').append(cleanString); }); ``` And here's some html: ``` <div id="content">original content</div> ``` Which gives me ``` <div id='content'> original content Free me! </div> ``` So my question to you, dear community, is how do I accomplish this #without# knowing anything about the div attributes?

Original source