I am working on copy to clipboard first time its working as expected but on second time without page refresh copy is not working. Can any one one suggest me, where i am going wrong ?
js function
function selectElementContents(el) {
var body = document.body, range, sel;
if (document.createRange && window.getSelection) {
range = document.createRange();
sel = window.getSelection();
sel.removeAllRanges();
try {
range.selectNodeContents(el);
sel.addRange(range);
} catch (e) {
range.selectNode(el);
sel.addRange(range);
}
} else if (body.createTextRange) {
range = body.createTextRange();
range.moveToElementText(el);
range.select();
}
document.execCommand("Copy");
}
cshtml
<a href="#" onclick="selectElementContents(document.getElementById('tbody'));">
<img alt="Copy Contents" style="height: 35px; width: 25px;" src="~/images/Copy_Contents.png" />
@Renderer.Localize("CopyContents")
</a>
Table
<table id="tableIOD" class="table table-hover">
<thead>
<tr>
<th scope="col">@Renderer.Localize("SearchUploadPopupValue")</th>
</tr>
</thead>
<tbody id="tbody">
@if (@Model.UploadSearchSummary != null && @Model.UploadSearchSummary.Count > 0)
{
foreach (var data in @Model.UploadSearchSummary.FindAll(c => c.Status.Equals(UploadSearchResultStatus.NotFound)))
{
<tr>
<td>@data.SearchValue</td>
</tr>
}
}
</tbody>
</table>
Rendered HTML
<div id="modal-search-by-upload" class="modal fade hide show" tabindex="-1" role="dialog" aria-modal="true" style="padding-right: 17px; display: block;">
<div id="modal-dialog" class="modal-dialog modal-lg">
<div id="modal-content" class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Search by upload - items not found</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div id="modal-body" class="modal-body">
<div id="phase1">
<table id="tableIOD" class="table table-hover">
<thead>
<tr>
<th scope="col">Value</th>
</tr>
</thead>
<tbody id="tbody">
<tr>
<td>5500002207330</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="tblfooter" class="modal-footer">
<a href="#" onclick="selectElementContents(document.getElementById('tbody'));">
<img alt="Copy Contents" style="height: 35px; width: 25px;" src="/images/Copy_Contents.png">
Copy Contents
</a>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
above code is rendered html code copy to clipboard button is inside model and i am trying to copy the data on model popup