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

              
                header.main-header
    h1 How to Add Text To Clipboard
    p In pure JavaScript, no need of that old Flash plugin. 
    
section.main-section
    p Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut non ex venenatis, egestas est vitae, lacinia libero. Sed pretium aliquet ultricies. Cras id semper nunc. Sed in leo eu nibh maximus bibendum. Cras pretium leo non lorem venenatis sollicitudin. Nulla sit amet condimentum erat. Aliquam mattis elementum mauris vel suscipit. In lacinia nec felis dictum vehicula. Aenean ut sem quis metus varius tempor nec non elit. Aliquam ac dolor nisi. Sed vitae leo odio.
    p Integer blandit, ex eu tristique eleifend, urna eros tempor leo, nec tincidunt risus purus sed massa. Pellentesque cursus eros id felis tincidunt eleifend. Nam dapibus semper diam ut porta. Proin lobortis tortor eu odio dapibus pellentesque. Quisque et laoreet massa. Nulla facilisi.
    
    p Sed purus ex, vulputate lacinia diam quis, porttitor molestie ligula. Phasellus feugiat erat quis justo laoreet viverra.
    
    p Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nulla a ullamcorper metus, non sagittis ipsum. Etiam quis est urna. Donec facilisis et nisi sit amet blandit. Cras aliquam condimentum nulla, nec malesuada eros imperdiet ut. Suspendisse potenti. Suspendisse tincidunt maximus enim, vel ultricies magna.
    
    textarea#entertext
    
    div.yupGuysThisIsAnAdvertisement
        span <b>Ad: </b>Cloud Hosting for Devs. on <a href='https://goo.gl/Flh2CZ'>Digital Ocean</a> <i> - get $10 for trial!</i>
              
            
!

CSS

              
                body {
  max-width:720px;
  margin:0 auto;
  padding:20px 20px;
  color:#333;
}

.main-header {
  text-align:center;
  margin-bottom:20px;
  padding:10px;
  border-bottom:1px solid #eee;
}

#entertext {
  width:100%;
  border:1px solid #ccc;
  padding:10px;
  height:250px;
}


.yupGuysThisIsAnAdvertisement {
  position:fixed;
  bottom:10px;
  left:10px;
  padding:5px;
  border:1px solid #4E81E7;
  box-shadow:2px 2px 0 #4E81E7;
  background:#fff;
  font-size:13px;
}

.yupGuysThisIsAnAdvertisement a {
  color:#fff;
  background:#4E81E7;
  text-decoration:none;
  padding:5px 5px;
  font-weight:600;
  text-shadow:1px 1px rgba(0,0,0,0.3);
}
              
            
!

JS

              
                
function addText() {
  
  // This is the text currently selected
  var selection = window.getSelection();
  
  // Manipulate the selected text as you wish, and store it in a new var
  var new_text = selection + "<br><br> Great cloud hosting at Digital Ocean: https://goo.gl/Flh2CZ";
  
  // Now create a new div
  var temp_div = document.createElement('div');
  
  // And insert the content of new_text in it
  temp_div.innerHTML = new_text;
  
  // Add an id
  temp_div.id = "new_div";
  
  // Hide it 
  temp_div.style.position = "absolute";
  temp_div.style.left = "-999999px";
  
  // Append it to the body
  document.body.appendChild(temp_div);
  
  
  // Make selection of the new element we created, and when you press CTRL+C it is the text that will get copied.
  selection.selectAllChildren(temp_div);
 
  
  // Remove the div we created, in 100ms
  window.setTimeout(function(){
    temp_div.parentNode.removeChild(temp_div.id);
    
    // The selection from the text the user selected is removed when they copy, this is because the selection is set on the new div that we created, if that's not acceptable in your case then you could use selection.setBaseAndExtent() to select the original text again. 
    
  }, 100);
  
}



// Add the event listener so on every copy event the addText() function is run
document.addEventListener('copy', addText);
              
            
!
999px

Console