Oct 5 2007

Onclick Select All and Copy to Clipboard (javascript)

javascript clipboard

In sometimes, in html , if you want to select the text for a textbox or a textarea , you can use the following script

<script type=”text/javascript”>

function select_all(obj)

{ var text_val=eval(obj);

text_val.focus();

text_val.select();

if (!document.all) return; // IE only

r= text_val.createTextRange();

r.execCommand(\\’copy\\’);

} </script>
The html part is:

<input value=”http://www.sajithmr.com” onclick=”select_all(this)” name=”url” type=”text” />The above script will select all the text for input field and copy to clipboard (for IE only)

TAGS:

18 Comments on this post

Trackbacks

  1. Brad said:

    Well this is just terrible. Just use this simple bit of code in the textbox or textarea.

    onFocus=”this.select();”

    Or maybe I’m just misinterpreting what your trying to do.

    April 19th, 2008 at 12:34 am
  2. Sajith M.R said:

    Brad,
    Here not only just select, also copy the content into your clipboard.

    (Similar to right click select all and copy)

    April 19th, 2008 at 12:48 am
  3. tonk said:

    goosebumps

    April 19th, 2008 at 12:41 pm
  4. Derive Host said:

    Great Post.

    Best Regards
    Team
    Web Hosting Sri Lanka

    May 5th, 2008 at 10:53 am
  5. Pigeon said:

    It doesn’t work, at least not in Opera, the line

    r.execCommand(\’copy\’);

    gives an inline script compilation error at the escaped quotes. If I unescape the quotes it gives a type mismatch error on the same line.

    May 6th, 2008 at 12:12 am
  6. topigeon said:

    What part of “The html part is:

    The above script will select all the text for input field and copy to clipboard (for IE only)” do you not understand? IE = Internet Explorer != Opera.

    June 2nd, 2008 at 5:07 pm
  7. selva said:

    how to copy a text in textbox to clipboard in mozilla???

    July 18th, 2008 at 4:24 pm
  8. Lindsay-Lohan said:

    thanks! very nice!

    July 31st, 2008 at 8:54 pm
  9. Anna8 said:

    Yes, great job. :) Interesting indeed.

    August 4th, 2008 at 10:54 pm
  10. Raja said:

    Check this link… working good…
    Copy to Clipboard

    August 28th, 2008 at 5:39 pm
  11. Neenbyday said:

    How i may contact admin this site? I have a question.
    iijiivei

    October 2nd, 2008 at 10:34 am
  12. Sajith M.R said:

    You can mail me at: admin@sajithmr.com

    October 2nd, 2008 at 3:22 pm
  13. MegranFuyis said:

    i newbie plz how us the baton “add me”?

    October 3rd, 2008 at 9:57 pm
  14. Narosponura said:

    All greetings! I have fallen in love today:)))))
    I wish to shout about love to all world ! :)
    Love this magnificent feeling:)))))

    October 27th, 2008 at 7:00 am
  15. Nick said:

    Nice one, this is really handy - thanks very much indeed. I prefer the this.select(); route myself as it’s so simple..

    Does anyone know an equally simple way to copy the contents to clipboard (in any browser)?

    Thanks very much indeed.

    November 6th, 2008 at 7:59 pm
  16. Sexualxe said:

    Hello you forum best. And Bye. just test. . :) :) :) :) :) :):) :) :) :) :) :)

    November 7th, 2008 at 11:52 pm
  17. sairiurse said:

    to: Admin - If You want to delete your site from my spam list, please sent url of your domain to my emai: stop.spam.today@gmail.com
    And I will remove your site from my base within 24 hours
    webmastegz

    November 15th, 2008 at 9:32 am
  18. HSCharles said:

    I have a flash site
    i’m looking for the script who shows google ads with flash.
    how can i get it?

    November 16th, 2008 at 7:11 am

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog

Sponsors

    Advt on sajithmr.com
    Itslife Online
    Advt on sajithmr.com
    Advt on sajithmr.com

Recent Comments

  • Binny V A: Thanks for the post - love the picture ;-)
  • TheAnand: A lot of people are seeing errors with google video chat….is there any other software which has to be...
  • Alex: Wow, interresting analysis you have done! I’m trying to run GoogleVoiceAndVideoSetup on linux, using...
  • Mella Fitriansyah: Nice plugin, I will try to add this plugin in my blog…
  • Mella Fitriansyah: Nice Info, Sir i will reading your another post success for you

Recent Readers

JOIN MY COMMUNITY!

Recent Posts