views:

28

answers:

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

+2  A: 

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 http://stackoverflow.com/questions/1539641/copy-text-to-the-clients-clipboard-using-jquery

karim79
The accepted answer to that question suggests a plugin that uses Flash. Nevertheless, it works great.
BoltClock
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