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

10
  • 1
    @DejaVu There are no styles to follow so it doesn't matter. Commented Apr 9, 2014 at 8:21
  • 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 Commented Apr 9, 2014 at 8: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. Commented Apr 9, 2014 at 8: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... Commented Apr 9, 2014 at 8:27
  • 1
    @tim That's right, the issue is irrelevant. Commented Apr 9, 2014 at 8:28

3 Answers 3

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());
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, thats very short, clean and nice
2

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

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

2 Comments

And then elem.html() or what?
you would do .html() if you want elem's html
0

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

1 Comment

Thanks, but overhead in this case :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.