Sadly, we are not allowed to copy to clipboard directly from JS for security reasons. There are tons of workarounds but more often than not, you should just tell them to use Cntrl+C for themselves. Also I think my reply is a little late. lol. You probably already figured this out!
In the end, I went with instructions to CTRL+C/CTRL+P as accessing the clipboard ended up sounding like a bit of a nightmare. If it’s good enough for YouTube, it’s good enough for me.
Don’t rely exclusively on the mouse for driving events, use the generic approach because the keyboard drives events too (Some people cycle through fields using the TAB key much faster than using the mouse). XBox controller is relevant to this scenario too.
This also has the added benefit of not selecting all text when the user wants to drag select just a portion of the text in the textarea. The original solution selects all text, all the time, which can be frustrating for users that only want to select some of the text.
I’m trying to do the same thing – add a button or text next to the text area saying ‘Copy Text’ – searching google keeps bringing up this page though… please help me out @jaycameron
Works fine. Except tabbing. Clicking it becomes editable but when i use the tab key on my keyboard its not highlighted or editable. Any idea how to fix this? thanks
Does anyone have a follow-up to Dody’s post? I also need this to work on pre code. I am trying to make this select my banner code. Simply put, an img tag that links using a basic a href tag. Absolutely nothing fancy.
So much easier then with jQuery. Just goes to show, a quick search can save you a lot of time, and KB on the finished site. Thanks Chris!
thx a lot!!! its really helpfull, another trick is sucks!!!
nice one
for designer its a very easy with help
WORKS !!!!
surf http://www.rapidmaza.com for sending free sms 400 character and for free sms api
thanks… its work! ^^
Great and Works! Thank you.
Just what I was looking for… thanks!
Do you also have the code to make it automatically copy to the clipboard? That would make my life complete lol.
cheers
Stef
just write document.execCommand(‘copy’); after this.select()
Sadly, we are not allowed to copy to clipboard directly from JS for security reasons. There are tons of workarounds but more often than not, you should just tell them to use Cntrl+C for themselves. Also I think my reply is a little late. lol. You probably already figured this out!
This doesn’t work in iOS devices. I’ve seen mention of setSelectionRange() working there, but I can’t get that to work in iOS: http://jsfiddle.net/zBa5n/2/
Does anyone know how to do this?
Works for me. Thanks!
@Stef: I looked for what you asked about as well and found this article: http://brooknovak.wordpress.com/2009/07/28/accessing-the-system-clipboard-with-javascript/
In the end, I went with instructions to CTRL+C/CTRL+P as accessing the clipboard ended up sounding like a bit of a nightmare. If it’s good enough for YouTube, it’s good enough for me.
This one is on mouseover:
*textarea cols=”35″ rows=”3″ onmouseover=”this.focus()” onfocus=”this.select()”>
TEXT HERE
*/textarea>
Much simpler and more correct:
onfocus=”this.select()”
Don’t rely exclusively on the mouse for driving events, use the generic approach because the keyboard drives events too (Some people cycle through fields using the TAB key much faster than using the mouse). XBox controller is relevant to this scenario too.
This also has the added benefit of not selecting all text when the user wants to drag select just a portion of the text in the textarea. The original solution selects all text, all the time, which can be frustrating for users that only want to select some of the text.
The code works like a charm! Thanks, I have been looking all over the internet for something like this!
ohh it was helpful
how about auto highlight “pre code” can you give guidance? thanks
What about ajax loaded textareas? They’ll pretty much fail.
It worked for me. thanks
Thanks for this, it helps too!
This only replaces the first instance can you replace all matching words?
sorry wrong thread
Does not work on chrome.
nope! I just rested it just a couple of minutes ago. It is working on Chrome. Check your browser whether JS enabled!
I am trying to add copy to clipboard function to my wordpress site. any suggestion?
Thanks, I am using this I want to change what is showing and one click selectable in my text area, based on a checkbox.
If you look here (http://indiegospel.org/widget), you see a widget page. My widget has a “Get this widget” tag that links to the widget page to make it “viral” or “shareable”. I would like to make that link optional with a checkbox so that the “Get this widget” tag is displayed or not displayed in the text area depending on user choice.
There is a working example here with the checkbox which controls the showing of the “Grab This” code in this Feedburner widget – http://feedburner.google.com/fb/a/headlineanimator/install?target=code&id=2392250btql00tmh66lqp000ms&w=2
I am basically trying to do the same sort of thing they are doing with my widget.
works Great
thanks
and I have one question, how can I add Button to make a copy of the selected Text?
I’m trying to do the same thing – add a button or text next to the text area saying ‘Copy Text’ – searching google keeps bringing up this page though… please help me out @jaycameron
Works fine. Except tabbing. Clicking it becomes editable but when i use the tab key on my keyboard its not highlighted or editable. Any idea how to fix this? thanks
I know it was posted a while ago, but did you ever get a response to this?
I am looking to do the same thing, when tabbing! Can’t figure it out.
Thanks
Hi,
Thank you,
It works everywhere even in chrome!
Thanks.
Does anyone have a follow-up to Dody’s post? I also need this to work on pre code. I am trying to make this select my banner code. Simply put, an img tag that links using a basic a href tag. Absolutely nothing fancy.
Thanks for this snippet. Does what I want.
Great, it works!
Thanks
is there any way to copy the text area in clipboard
Thanks. It workes on my blog. Check
http://www.brupak.com.np/2015/03/disable-copy-and-right-click-on-blog.html
Works:
https://jsfiddle.net/PianoGuy324/8vgssn93/
Did anyone find a solution for Safari?? for me it doesn’t work….