PDA

View Full Version : [Help needed] Copy & Paste



Slobrob
08-05-2011, 11:25 PM
Hi guys,

I'm pretty much a newbie when it comes to javascript and i'm still getting to grips with it all but i am in some need of some help.

I'm doing a relatively simple project for a friend of mine and what he is wanting is a simple web page with several buttons that have different information on them.

As you click each button it will automatically copy and then paste into a text area with each bit of information on a new line. I think I have grasped the copy and paste but I am a bit unsure as to how I copy each button and automatically paste into the text area.

once the relevant information is pasted into the textbox - he then wants a button to copy all of that information which can be pasted into a different system.

Here is my code that I have at the moment


<script type="text/javascript">

function copy()

{

Copytext = document.getElementById("button").value;

Copytext.execCommand("Copy");

}

function paste()

{

document.form.textarea.focus();

PasteText = document.form.textarea.createTextRange();

PasteText.execCommand("Paste");

}

</script>

<body>

<form name="form">

<input type="button" onClick="copy()" value="Option 1" />
</br>
<input type="button" onClick="copy()" value="Option 2" />
</br>
<input type="button" onClick="copy()" value="Option 3" />
</br>
<input type="button" onClick="copy()" value="Option 4" />
</br>
<input type="button" onClick="copy()" value="Option 5" />
</br>
<input type="button" onClick="copy()" value="Option 6" />

<p></p>

<textarea id="textarea" cols="60" rows="5"></textarea>
<br />
<input type="button" onClick="copy()" value="Copy text to clipboard" />
</form>

Any help would be appreciated

JShor
08-06-2011, 03:54 PM
There are lots of problems with copy/paste in different browsers for various security reasons. One bit of code may work in IE, but not other browsers. One piece of code might work in FF, but not IE, Chrome, etc.

The best solution is to use zeroclipboard:
http://code.google.com/p/zeroclipboard/

I use it myself, and it is clean, cross-browser and easy to install/use.

Slobrob
08-06-2011, 11:59 PM
Hi JShor,

Thanks for the advice but its not what im looking for. The wbe browser he will be using is IE anyway and ive managed to work it out using the following code


<script language="JavaScript">
function clipboard()
{
var copy = document.getElementById('copy').createTextRange();
copy.select();
copy.execCommand("Copy");

}

function paste()
{
document.getElementById('text').focus();
var paste = document.selection.createRange();
paste.execCommand("Paste");
}
</script>



<TEXTAREA ID="text" style="height:100px; width:250px;">
</TEXTAREA>
<BUTTON id="copy"onClick="clipboard(); paste();">Option 1</BUTTON>

cheers anyway!