yegor256
yegor256

Reputation: 105210

how to get inner HTML with jquery 2.0?

This is my code (jquery 2.0.3):

alert($('<p>test <b>me</b></p>').filter('p').get(0).html());

Chrome says:

Cannot call method 'html' of undefined 

What's wrong?

Btw, I'm expecting test <b>me</b>.

ps. My problem is that I'm running this from XSLT, not HTML. But thanks everybody for your answers.

Upvotes: 1

Views: 147

Answers (2)

callmehiphop
callmehiphop

Reputation: 646

If you were to use .get(0) then you would get back the actual DOM element, not the jQuery object. So you'd have to use standard DOM stuff.. in this case

$('<p>test <b>me</b></p>').filter('p').get(0).innerHTML;

Otherwise if you wanted to keep it all jQuery, you could say

// based on the .get(0) I'm assuming you want the first matched element?
$('<p>test <b>me</b></p>').filter('p').eq(0).html();

And to grab every paragraph's html

$('<p>test <b>me</b></p>').filter('p').html();

Upvotes: 1

Christopher Marshall
Christopher Marshall

Reputation: 10736

Remove the .get(0) method.

http://jsfiddle.net/NMhQW/

alert($('<p>test <b>me</b></p>').filter('p').html());

Upvotes: 6

Related Questions