1

i have a input[type=text] containing the short url for a post. i want to select the short url so users can easily copy the short url into clipboard. i used

$(".shorturl input").focus(function() {
    this.select();
});

but i noticed the 1st time it works fine then the next time it will blick (i see the text selected then deselected). it seems when like it try to select a selected text and ends up deselecting?

then to enhance this, how can i copy text to the clipboard? hopefully without flash? i see jQuery plugins to copy text but they use flash.

my site using that is http://jiewmeng.tumblr.com

Jiew Meng
  • 84,767
  • 185
  • 495
  • 805

1 Answers1

2

Try using the click event instead. It seems to work when focusing on the input using the keyboard as well, but I haven't tested it cross-browser:

$(".shorturl input").click(function() {
    this.select();
});​

Demo at http://jsfiddle.net/mZSyh/

For the second part of your question, see How to copy text to the client's clipboard using jQuery?

Community
  • 1
  • 1
karim79
  • 339,989
  • 67
  • 413
  • 406
  • The accepted answer to that question suggests a plugin that uses Flash. Nevertheless, it works great. – BoltClock Jul 16 '10 at 05:12
  • Well, you can't copy text onto the clipboard with pure JavaScript, so it has to use Flash or some other plug-in. – Tim Down Jul 16 '10 at 08:36