4

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

tim
  • 9,896
  • 20
  • 81
  • 137
  • shouldn't you end `display: none` with a `;`? – Déjà vu Apr 09 '14 at 08:20
  • 1
    @DejaVu There are no styles to follow so it doesn't matter. – George Apr 09 '14 at 08:21
  • I didn't understand what you wanted to say @Deja Vu, but nevermind, as oGeez clarified, it probably won't matter anyway – tim Apr 09 '14 at 08:21
  • @oGeez Alright, but it looks cleaner for me if it ends with a `;`. It was just a suggestion – Déjà vu Apr 09 '14 at 08:22
  • Can you put jsfiddle? – Just code Apr 09 '14 at 08:24
  • What to end with `;`? – tim Apr 09 '14 at 08:24
  • 1
    @tim He means, quite rightly, that styles should be separated by a semi-colon. Although the last property in the block doesn't need one. More [here](http://stackoverflow.com/questions/11939595/leaving-out-the-last-semicolon-of-a-css-block) – George Apr 09 '14 at 08:25
  • 1
    @tim — He is suggesting that you use `;` as a rule terminator in your CSS instead of as a rule separator. It has no bearing on your actual problem. – Quentin Apr 09 '14 at 08:26
  • 1
    Ah I see, sure I know that, but for one property, it's not necessary, is it? And yes, actually no relation to my problem in any way... – tim Apr 09 '14 at 08:27
  • 1
    @tim That's right, the issue is irrelevant. – George Apr 09 '14 at 08:28

3 Answers3

4

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());
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

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

elem = $('<div />',{
  html: aData[3];
}).find("div");
alert(elem);
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
0

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

cor
  • 3,323
  • 25
  • 46