Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the full table html from Jquery? [duplicate]

Tags:

html

jquery

I am trying to get the table html in Jquery.

I have the following:

<table id='table' border='1'>
<tr><td>items...</td><td>items2...</td></tr>
<tr><td>items...</td><td>items2...</td></tr>
<tr><td>items...</td><td>items2...</td></tr>
<tr><td>items...</td><td>items2...</td></tr>
</table>

I have

var test = $('#table').html()

but when i console.log(test) i got

<tr><td>items...</td><td>items2...</td></tr>
<tr><td>items...</td><td>items2...</td></tr>
<tr><td>items...</td><td>items2...</td></tr>
<tr><td>items...</td><td>items2...</td></tr>

without <table> tags.

Is there anyway to fix this? Thanks a lot!

like image 489
FlyingCat Avatar asked Nov 30 '22 12:11

FlyingCat


2 Answers

A more jQuery oriented approach would be to retrieve the outerHtml property from the table.

 $('#table').prop('outerHTML')
like image 184
Stieffers Avatar answered Dec 21 '22 19:12

Stieffers


One option is:

document.getElementById('table').outerHTML;

JS Fiddle demo.

Or, you could instead:

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

JS Fiddle demo.

like image 22
David Thomas Avatar answered Dec 21 '22 21:12

David Thomas