Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container">
  <h2>Accessing the Clipboard using execCommand()</h2>
  
  <h3>Copy</h3>
  <p id="content-copy">This text will be inserted into the clipboard.</p>
  <button id="button-copy">Copy Text</button>
  
  <h3>Cut</h3>
  <input id="content-cut" type="text" value="This text will be cut, then inserted into the clipboard." />
  <button id="button-cut">Cut Text</button>
  
  <h3>Paste</h3>
  <textarea placeholder="Press Ctrl + V to paste the clipboard's current content." rows="10"></textarea>
</div>
              
            
!

CSS

              
                
              
            
!

JS

              
                document.addEventListener("DOMContentLoaded", () => {
  const $ = document.querySelector.bind(document);
  const contentCopyElm = $("#content-copy"),
        inputCutElm = $("#content-cut"),
        buttonCopyElm = $("#button-copy"),
        buttonCutElm = $("#button-cut");
  
  const isSupported = (cmd) => {
    return document.queryCommandSupported(cmd);
  }
  
  buttonCopyElm.addEventListener("click", () => {
    // We will need a range object and a selection.
    const range = document.createRange(),
          selection = window.getSelection();

    // Clear selection from any previous data.
    selection.removeAllRanges();

    // Make the range select the entire content of the element.
    range.selectNodeContents(contentCopyElm);

    // Add that range to the selection.
    selection.addRange(range);

    // Try to copy the selection to the clipboard.
    try {
      if(isSupported("copy")) document.execCommand("copy");
      else alert(`execCommand("copy") is not supported in your browser.`);
    } catch(e) {
      console.log(e);
    }
    
    // Clear selection if you want to.
    selection.removeAllRanges();
  });
  
  buttonCutElm.addEventListener("click", () => {
    // Select the content of the input
    inputCutElm.select();
    
    // Try to cut the content of the input
    try {
      if (isSupported("cut")) document.execCommand("cut");
      else alert(`execCommand("cut") is not supported in your browser.`);
    } catch(e) {
      console.log(e);
    }
  });
});

/**
* https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Interact_with_the_clipboard
* https://alligator.io/js/copying-to-clipboard/
* https://codepen.io/chrisdavidmills/full/gzYjag/
* https://tutorialzine.com/2016/10/quick-tip-accessing-the-clipboard-with-javascript
*/
              
            
!
999px

Console