Site icon Dotnet Helpers

Copy data from div to ClipboardData using Jquery

How to copy div content form div to clipboard

Code Behind File :

Create string builder with some content and assign as like

divcontent.InnerHtml = sbQuestionAndAnswers.ToString();

Design File:

 <div id=”divcontent” style=” width: 1px; height: 1px;overflow:hidden;” runat=”server”></div>

Script File :

if (window.clipboardData && clipboardData.setData) {
if ($(‘#ctl00_cphMain_divcontent’)) {
$(‘#ctl00_cphMain_divcontent’).show();
var editableDiv = $(‘#ctl00_cphMain_divcontent’)

[0];
var txtRange = document.body.createTextRange();
editableDiv.style.display = “”;
txtRange.moveToElementText(editableDiv);
txtRange.select();
txtRange.execCommand(“Copy”);
editableDiv.style.display = “none”;//Hide the div
}}

Note :

Copying the content of div which is bind dynamically form code-behind and it will not display in the page. From that we copy content to clipboard

Exit mobile version