John
John

Reputation:

How to get html of matched element in jquery

I have this html

<div id"test123">
dasd'asdasd

</div>

I want the content of that div box with "<div id="test123">" itself as well not just innerhtml

I tried $('#test123').html()

But it only gave me inner html

Upvotes: 0

Views: 463

Answers (4)

obiuquido144
obiuquido144

Reputation: 21

Another alternative, which looks more linear/nicer to me:

var html = $('#foo').wrap('<div/>').parent().html();
$('#foo').unwrap();
found here:
http://codeasp.net/blogs/raghav_khunger/microsoft-net/751/jquery-how-to-get-html-of-a-div-with-that-div-tag-included

Upvotes: 2

Kent
Kent

Reputation: 469

Pretty gnarly, but it gets the job done:

$('<div />').append($('#test123').clone()).html();

Upvotes: 0

Jason
Jason

Reputation: 52523

var div = $('#test123').eq(0);
console.log(div);

Upvotes: 0

liammclennan
liammclennan

Reputation: 5368

Doesn't seem to be an easy way in the box.

http://brandonaaron.net/blog/2007/06/17/jquery-snippets-outerhtml/

Upvotes: 1

Related Questions