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!
A more jQuery oriented approach would be to retrieve the outerHtml property from the table.
$('#table').prop('outerHTML')
One option is:
document.getElementById('table').outerHTML;
JS Fiddle demo.
Or, you could instead:
$('<div />').html($('#table').clone()).html();
JS Fiddle demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With