Jiew Meng
Jiew Meng

Reputation: 88357

Small bug when selecting text on focus

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

Upvotes: 1

Views: 160

Answers (1)

karim79
karim79

Reputation: 342765

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?

Upvotes: 2

Related Questions