airnet
airnet

Reputation: 2673

jquery html() -> TypeError: this[0].innerHTML is undefined

I have an element:

var node = $(xml).find('target');

node is something like <content><item><target>Breadcrumbs</target></item> </content>. So node is actually an xml element

And I want to strip the <content> node, but keep everything else.

I thought $(node).html() would work . but I get an error. Why wouldn't this work?

Upvotes: 1

Views: 3481

Answers (2)

Toby
Toby

Reputation: 108

I have the exact same problem with jquery and ie, when trying to get html() for an element. Reason is that xml Element does not define innerHTML in IE.

I found a temporary solution with innerXHTML code here:

http://www.stevetucker.co.uk/page-innerxhtml.php

It allows you to send the element to a function with name innerXHTML and return valid html.

Upvotes: 0

BNL
BNL

Reputation: 7133

This works when you add quotes to the string:

var node = "<content><item><target>Breadcrumbs</target></item> </content>";
alert($(node).html());

Result:

<item><target>Breadcrumbs</target></item>

http://jsfiddle.net/hEtgH/

Assuming your node actually is pulled out of the dom, it would be similar.

<content id="blah"><item><target>Breadcrumbs</target></item> </content>

Then:

alert($("#blah").html());

Result:

<item><target>Breadcrumbs</target></item>

http://jsfiddle.net/YrLfu/

Upvotes: 1

Related Questions