copy table or div content on button click using jquery

copy table or div content on button click using jquery



I'm getting table html $(selector).html()


$(selector).html()



But after execute document.execCommand("copy"); it does not copy the content of the table.


document.execCommand("copy");



Please find attachment for more clearness.enter image description here




function copy(selector)
var $temp = $("<div>");
$("body").append($temp);
$temp.attr("contenteditable", true)
.html($(selector).html()).select()
.focus(function()
document.execCommand('selectAll', false, null)
);
document.execCommand("copy");
$temp.remove();


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>

<tbody>
<tr>
<td colspan="2" style="text-align: left; line-height: 40px; font-family: Verdana; font-size: 12px; font-weight: bold; color: #123456; text-transform: uppercase; background: #e6d05c75;">Accomodation</td>
</tr>
<tr>
<td width="50%">Country</td>
<td>Mauritius</td>
</tr>
<tr>
<td>Destination</td>
<td>All Hotels</td>
</tr>
<tr>
<td width="50%">Hotel Name</td>
<td>Anahita Mauritius - The Resort</td>
</tr>
<tr>
<td>Check In</td>
<td>22/Aug/2018</td>
</tr>
<tr>
<td>Check Out</td>
<td>24/Aug/2018</td>
</tr>
<tr>
<td colspan="2">
<table cellpadding="0px" cellspacing="0" width="100%" style=" padding:20px; line-height: 30px; font-family: Verdana; font-size: 12px; font-weight: bold; color: #123456; vertical-align: top" rules="all">
<tbody>
<tr style="background: #cce7ff;">
<td>Room Type</td>
<td>Meal Type</td>
<td>Offer</td>
</tr>
<tr>
<td>1 Bedroom Suite Garden(02 pax)</td>
<td>Half Board</td>
<td>N/A</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr style="background: #c0e098;font - size: 15px ">
<td>Total Package Cost</td>
<td>1502.00 USD</td>
</tr>
<tr>
<td><input readonly="" type="text" value="Click Me To Copy" class="allowCopy noselect"></td>
<td><button onclick="copy(tblcopy)" type="button">Click to Copy</button></td>
</tr>
</tbody>
</table>





I made you a snippet. Please fix the console error - perhaps you mean copy('#tblcopy') and have an ID="tblcopy" on the html that you did not show?
– mplungjan
Aug 22 at 8:19



copy('#tblcopy')





Why did you edit my snippet. It is really useful to have!!! Please keep it there and fix your HTML
– mplungjan
Aug 22 at 8:21






Do you get any errors in the console? Does it copy anything at all?
– Oram
Aug 22 at 8:23





Currently there is no var tblcopy available to the page
– mplungjan
Aug 22 at 8:24





From close vote: "Questions seeking debugging help ("why isn't this code working?") must include the desired behaviour, a specific problem or error and the shortest code necessary to reproduce it.". In your case, your code does not work (see mplungjan's comments), so we can't see if it's the copy not working or just a typo. This would be clearer if you provided error messages from the console rather than just "it's not working" (ie is it the missing variable/selector or is it the copy itself not working?)
– freedomn-m
Aug 22 at 8:25




1 Answer
1



You might want to try this.



I based the selection of the text from this post Answered by @Tom Oakley




function selectText(el)
var doc = document;
var element = document.getElementById(el);
if (doc.body.createTextRange)
var range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
else if (window.getSelection)
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);


function copy(selector)
var $temp = $("<div id='toCopy'>");
$("body").append($temp);
$temp.attr("contenteditable", true)
.html($(selector).html());
selectText("toCopy");
document.execCommand("copy");
$temp.remove();


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tblcopy">

<tbody>
<tr>
<td colspan="2" style="text-align: left; line-height: 40px; font-family: Verdana; font-size: 12px; font-weight: bold; color: #123456; text-transform: uppercase; background: #e6d05c75;">Accomodation</td>
</tr>
<tr>
<td width="50%">Country</td>
<td>Mauritius</td>
</tr>
<tr>
<td>Destination</td>
<td>All Hotels</td>
</tr>
<tr>
<td width="50%">Hotel Name</td>
<td>Anahita Mauritius - The Resort</td>
</tr>
<tr>
<td>Check In</td>
<td>22/Aug/2018</td>
</tr>
<tr>
<td>Check Out</td>
<td>24/Aug/2018</td>
</tr>
<tr>
<td colspan="2">
<table cellpadding="0px" cellspacing="0" width="100%" style=" padding:20px; line-height: 30px; font-family: Verdana; font-size: 12px; font-weight: bold; color: #123456; vertical-align: top" rules="all">
<tbody>
<tr style="background: #cce7ff;">
<td>Room Type</td>
<td>Meal Type</td>
<td>Offer</td>
</tr>
<tr>
<td>1 Bedroom Suite Garden(02 pax)</td>
<td>Half Board</td>
<td>N/A</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr style="background: #c0e098;font - size: 15px ">
<td>Total Package Cost</td>
<td>1502.00 USD</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</tbody>
</table>

<button onclick="copy('#tblcopy')" type="button">Click to Copy</button>





thanks its working fine @Neil Villareal
– sushil
Aug 23 at 4:24






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế