Cut & Paste

S

Simo

I use these two functions for Copy & Paste with buttons in some text area
into a form:

<SCRIPT LANGUAGE="Javascript">

function copy(area){
var temp = eval("document." + area);
aa=temp;
window.clipboardData.setData('Text',aa);
}

function paste(area2){
var temp2 = eval("document." + area2);
bb=window.clipboardData.getData('Text');
temp2 = bb;
}
</SCRIPT>
</head>


If i call the Copy function (dinamically with php) with a parameter like:
form1.text1.value it works fine, but with the Paste function doesn't work.
I think the problem is in the evaluate statement of the parameter area2.

Anyone can help me ?? Thanks

Simone
 
E

Evertjan.

Simo wrote on 28 apr 2004 in comp.lang.javascript:
<SCRIPT LANGUAGE="Javascript">

function copy(area){
var temp = eval("document." + area);
aa=temp;
window.clipboardData.setData('Text',aa);
}

function paste(area2){
var temp2 = eval("document." + area2);
bb=window.clipboardData.getData('Text');
temp2 = bb;
}
</SCRIPT>

Try this:

<div id=from>qwerty</div>
===========================
<div id=to>asdfg</div>

<script type="text/javascript">

function copy(area){
var temp = document.getElementById(area);
window.clipboardData.setData('Text',temp.innerHTML);
}

function paste(area){
var temp = document.getElementById(area);
temp.innerHTML = window.clipboardData.getData('Text');
}

copy("from")
paste("to")


</script>

Tested IE6
 
B

Brian Genisio

Simo said:
I use these two functions for Copy & Paste with buttons in some text area
into a form:

<SCRIPT LANGUAGE="Javascript">

function copy(area){
var temp = eval("document." + area);
aa=temp;
window.clipboardData.setData('Text',aa);
}

function paste(area2){
var temp2 = eval("document." + area2);
bb=window.clipboardData.getData('Text');
temp2 = bb;
}
</SCRIPT>
</head>


If i call the Copy function (dinamically with php) with a parameter like:
form1.text1.value it works fine, but with the Paste function doesn't work.
I think the problem is in the evaluate statement of the parameter area2.

Anyone can help me ?? Thanks

Simone

Wow... where do I start?

1. Do not use LANGUAGE. Instead, use TYPE
2. Do not use eval(). There are always better ways to do it
3. Dont make so many temp variables. It makes it hard to read
4. clipboardData only works in IE 5+. I dont know if anything like this
exists in other browsers.

Try something more like this. Of course, modify it for a better set of
what you need, but this should give you a good start.

-- Brian

<HTML>
<HEAD>
<SCRIPT type="text/javascript">

function copy(source){
window.clipboardData.setData("Text",
document.getElementById(source).value);
}

function paste(dest){
document.getElementById(dest).value =
window.clipboardData.getData("Text");
}

function CopyAndPaste( from, to )
{
copy(from); paste(to);
}

</SCRIPT>
</HEAD>

<BODY>
<FORM action="submit.asp">
<INPUT TYPE=text name=text1>Text 1<BR>
<INPUT TYPE=text name=text2>Text 2<BR>
</FORM>

<BUTTON onClick="CopyAndPaste('text1', 'text2')">
Copy and Paste
</BUTTON>

</BODY>
</HTML>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top