Cut, Copy, and Paste text from a Web Page to Clipboard

Author:  MikeW Date: 2016-01-13T22:46:49+00:00

Want to make it easy to copy and paste some random piece of text to the clipboard? Well this can be accomplished using HTML with surprising ease. All you need to do is add a little JavaScript. Here are two examples, one with text box and one with a text area.

This trick can be used for any bit of text you want to copy to the clipboard a lot. Very useful for storing an e-mail address or user name that you need to copy and paste repeatedly.

Examples

These examples copy a command line for the Maven build tool.

Text Box Example

Source

<input onclick="this.select();" type='text' size="40" value='mvn archetype:generate -DgroupId=com.example -DartifactId=app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false' />

Text Area Example

Source

<textarea cols="50" rows="4" onclick="this.select();">mvn archetype:generate -DgroupId=com.example -DartifactId=app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false</textarea>