copy paste cross-browser

Posted in JavaScript by

One of the most used features of a modern pc is copy and pasting. Every browser got support for it, sometimes however you want that your users to copy exactly one piece of string and use a bit javascript to help them.

This solution is simple, it does not use flash or depend on browser specific javascript. It still one of the best solutions. Below are the html and javascript snippets for it. Enjoy.

html snippet

<form>
...
<input id="carecopy" value="#care" />
...

javascript snippet

document.getElementById("carecopy").focus();
document.getElementById("carecopy").select();
//Press CTRL-C now

You can use it with jquery $("#carecopy") or prototype $("carecopy").activate(). If you do not want to show the input field, you can use the style option and hide it.

Published at , Updated at 2013-03-02

next: Ender prev: Introduction to JavaScript