medihack
medihack

Reputation: 16627

Get content of DOM element without the parent by using jQuery

Here are some simple examples what I am trying to do:

$("<li>Foo</li>")
    => "Foo"
$("<li><span>Foo</span></li>")
    => <span>Foo</span>
$('<li class="foo"><div>Foo</div><div>Bar</div></li>')
    => <div>Foo</div><div>Bar</div>

I know of the jQuery get() method, but this just gives me the <li> elments, too. A .children().get() does not work (at least in the first case). Is there a simple way to achieve that (with built in jQuery methods)?

Upvotes: 2

Views: 613

Answers (2)

Matt Bridges
Matt Bridges

Reputation: 49385

To get the children of a node including text nodes, use the contents() method instead of children().

Upvotes: 2

AndreKR
AndreKR

Reputation: 33658

$("<li>Foo</li>").html()
$("<li><span>Foo</span></li>").html()
$('<li class="foo"><div>Foo</div><div>Bar</div></li>').html()

Upvotes: 4

Related Questions