Onclick Select All and Copy to Clipboard (javascript)
![]()
In sometimes, in html , if you want to select the text for a textbox or a textarea , you can use the following script
function select_all(obj)
{ var text_val=eval(obj);
text_val.focus();
text_val.select();
if (!document.all) return; // IE only
r= text_val.createTextRange();
r.execCommand(\\\'copy\\\');
} <script type=”text/javascript”>
function select_all(obj)
{ var text_val=eval(obj);
text_val.focus();
text_val.select();
if (!document.all) return; // IE only
r= text_val.createTextRange();
r.execCommand(\\’copy\\’);
} </script>
The html part is:
<input value=”http://www.sajithmr.com” onclick=”select_all(this)” name=”url” type=”text” />The above script will select all the text for input field and copy to clipboard (for IE only)
|
|
|
|
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
Stumble Reviews
Plugin powered by StumbleCult10 Responses to “Onclick Select All and Copy to Clipboard (javascript)”
-
Brad Says:
April 19th, 2008 at 12:34 amWell this is just terrible. Just use this simple bit of code in the textbox or textarea.
onFocus=”this.select();”
Or maybe I’m just misinterpreting what your trying to do.
-
Sajith M.R Says:
April 19th, 2008 at 12:48 amBrad,
Here not only just select, also copy the content into your clipboard.(Similar to right click select all and copy)
-
tonk Says:
April 19th, 2008 at 12:41 pmgoosebumps
-
Derive Host Says:
May 5th, 2008 at 10:53 amGreat Post.
Best Regards
Team
Web Hosting Sri Lanka -
Pigeon Says:
May 6th, 2008 at 12:12 amIt doesn’t work, at least not in Opera, the line
r.execCommand(\’copy\’);
gives an inline script compilation error at the escaped quotes. If I unescape the quotes it gives a type mismatch error on the same line.
-
topigeon Says:
June 2nd, 2008 at 5:07 pmWhat part of “The html part is:
The above script will select all the text for input field and copy to clipboard (for IE only)” do you not understand? IE = Internet Explorer != Opera.
-
selva Says:
July 18th, 2008 at 4:24 pmhow to copy a text in textbox to clipboard in mozilla???
-
Lindsay-Lohan Says:
July 31st, 2008 at 8:54 pmthanks! very nice!
-
Anna8 Says:
August 4th, 2008 at 10:54 pmYes, great job.
Interesting indeed. -
Raja Says:
August 28th, 2008 at 5:39 pmCheck this link… working good…
Copy to Clipboard


Review on — April 19, 2008, 12:02 am