{"id":1720,"date":"2023-11-06T05:00:00","date_gmt":"2023-11-06T13:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1720"},"modified":"2023-11-03T15:35:12","modified_gmt":"2023-11-03T22:35:12","slug":"copy-and-paste-in-javascript","status":"publish","type":"post","link":"http:\/\/www.angulartraining.com\/daily-newsletter\/copy-and-paste-in-javascript\/","title":{"rendered":"Copy and paste in JavaScript"},"content":{"rendered":"\n<p>Sometimes, it&#8217;s helpful to copy and paste some text programmatically. There are different options in Javascript, and the only one that works consistently is the following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"197\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/11\/image-4-1024x197.png\" alt=\"\" class=\"wp-image-1721\" srcset=\"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/11\/image-4-1024x197.png 1024w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/11\/image-4-300x58.png 300w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/11\/image-4-768x148.png 768w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/11\/image-4-1536x296.png 1536w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/11\/image-4.png 1714w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>You can see an example of the above <a href=\"https:\/\/stackblitz.com\/edit\/stackblitz-starters-znshkv?description=An%20angular-cli%20project%20based%20on%20@angular\/animations,%20@angular\/common,%20@angular\/compiler,%20@angular\/core,%20@angular\/forms,%20@angular\/platform-browser,%20@angular\/platform-browser-dynamic,%20@angular\/router,%20core-js,%20rxjs,%20tslib%20and%20zone.js&amp;file=src%2Fmain.ts&amp;title=Angular%20Starter\" target=\"_blank\" rel=\"noopener\" title=\"\">code in action on Stackblitz here<\/a>. The only caveat is that such code can be triggered from an event listener, such as <code>onClick<\/code>.<\/p>\n\n\n\n<p>A newer API is emerging but isn&#8217;t fully supported anywhere, and Firefox support is significantly lacking, so I wouldn&#8217;t recommend it. The main problem with that new approach is that it requires the user&#8217;s permission, and there&#8217;s currently no way to ask for it. The <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Permissions_API\" target=\"_blank\" rel=\"noopener\" title=\"\">Permissions API<\/a> seems to be a bit inconsistent and <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Permissions\" target=\"_blank\" rel=\"noopener\" title=\"\">lacking<\/a> at this point:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"854\" height=\"108\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/11\/image-5.png\" alt=\"\" class=\"wp-image-1722\" srcset=\"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/11\/image-5.png 854w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/11\/image-5-300x38.png 300w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/11\/image-5-768x97.png 768w\" sizes=\"auto, (max-width: 854px) 100vw, 854px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>It&#8217;s a shame because that new API would allow copying custom content into the clipboard, not just the current user&#8217;s selection.<\/p>\n\n\n\n<p>By the way, a quick reminder to tune in for the <a href=\"https:\/\/www.youtube.com\/watch?v=Wq6GpTZ7AX0\" target=\"_blank\" rel=\"noopener\" title=\"\">Angular live event today on Youtube<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, it&#8217;s helpful to copy and paste some text programmatically. There are different options in Javascript, and the only one that works consistently is the following: You can see an example of the above code in action on Stackblitz here. The only caveat is that such code can be triggered from an event listener, such [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-1720","post","type-post","status-publish","format-standard","hentry","category-javascript"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1720","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=1720"}],"version-history":[{"count":2,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1720\/revisions"}],"predecessor-version":[{"id":1724,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1720\/revisions\/1724"}],"wp:attachment":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1720"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}