tim
tim

Reputation: 10186

jQuery: Get inner HTML from a HTML code within a variable

I have a variable which contains a html element:

alert(aData[3]);

gives me:

BLA BLA BLA
<div style="display: none">
 <table>....</table>
</div>

I'm trying to figure out how to get the contents WITHIN the div-tag from the variable aData[3] via jQuery. I tried various things:

elem = $(aData[3]).find("div");
alert(elem.html());

// OR
elem = $(aData[3]).find("div");
alert(elem[0].html());

// OR
elem = $(aData[3]).find("div");
alert($(elem[0]).html());

// OR
elem = $(aData[3], 'div');
alert(elem.html());

Can't get any of those to work. How to the correct version? :( Thanks a lot

Upvotes: 4

Views: 2562

Answers (3)

cor
cor

Reputation: 3393

You can use parseHTML to convert the string into an array of DOM elements.

Upvotes: 0

Quentin
Quentin

Reputation: 944538

find looks for descendants of elements in the jQuery object, but div is the highest level element in your HTML (it isn't a descendant of anything).

Just don't use find.

elem = $(aData[3]);
alert(elem.html());

Upvotes: 4

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85653

You need to wrap your string with another dom object to use .find().

elem = $('<div />',{
  html: aData[3];
}).find("div");
alert(elem);

Upvotes: 2

Related Questions