canadiancreed
canadiancreed

Reputation: 1984

copying textarea text regardless of browser

I"m trying to do something that I originally thought would be quite simple, but it's turning out not to be. Mainly I'm trying to have so I can have a textarea on a page, with a button that would allow the user to copy everything within the textarea to paste wherever they wish. However after slogging through a few attempts and a lot of applications of google-fu, I've yet to have an example that works, much less is cross browser compatible.

So I guess my question is two fold; is this possible, and if so, is this able to be done so that the browser used isn't a factor?

Thanks for your help and responses to this question.

Upvotes: 1

Views: 154

Answers (1)

Dan Herbert
Dan Herbert

Reputation: 103387

This is possible, but isn't consistent across browsers. You can use the jQuery plugin Clipboard as a substitute.

This plugin uses a Flash object internally to copy to the clipboard, which should be consistent across browsers.

With the plugin included, the following code should work:

var text = $("textarea").text();
$.clipboard(text);

That will copy everything in the text area to the clipboard.

Upvotes: 1

Related Questions