I want show the table code in textarea when page loads. My following code doing that for me but I want my showed code must have opening and closing table tag. Please tell me how I can do that!
var elem = document.getElementById("myTable");
var targetId = "_hiddenCopyText_";
var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
var origSelectionStart, origSelectionEnd;
target = elem;
origSelectionStart = elem.selectionStart;
origSelectionEnd = elem.selectionEnd;
document.getElementById("showTableCode").value=elem.innerHTML.replaceAll("<tbody>", '').replaceAll("</tbody>", '').replace(/(\r\n|\r|\n){2,}/g, '\n');
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
<html>
<head>
</head>
<body onload="myFunction()">
<center>
<table id="myTable">
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
</table></center>
<br>
<center><b>Your Table Code:</b><br>
<textarea cols="50" id="showTableCode" rows="10">
</textarea></center>
</body>
</html>