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)

45 Comments

45 Responses to “Onclick Select All and Copy to Clipboard (javascript)”

  1. Brad April 19, 2008 at 12:34 am #

    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.

  2. Sajith M.R April 19, 2008 at 12:48 am #

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

    (Similar to right click select all and copy)

  3. tonk April 19, 2008 at 12:41 pm #

    goosebumps

  4. Derive Host May 5, 2008 at 10:53 am #

    Great Post.

    Best Regards
    Team
    Web Hosting Sri Lanka

  5. Pigeon May 6, 2008 at 12:12 am #

    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.

  6. topigeon June 2, 2008 at 5:07 pm #

    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.

  7. selva July 18, 2008 at 4:24 pm #

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

  8. Lindsay-Lohan July 31, 2008 at 8:54 pm #

    thanks! very nice!

  9. Anna8 August 4, 2008 at 10:54 pm #

    Yes, great job. :) Interesting indeed.

  10. Raja August 28, 2008 at 5:39 pm #

    Check this link… working good…
    Copy to Clipboard

  11. Neenbyday October 2, 2008 at 10:34 am #

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

  12. Sajith M.R October 2, 2008 at 3:22 pm #

    You can mail me at: admin@sajithmr.com

  13. MegranFuyis October 3, 2008 at 9:57 pm #

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

  14. Narosponura October 27, 2008 at 7:00 am #

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

  15. Nick November 6, 2008 at 7:59 pm #

    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.

  16. Sexualxe November 7, 2008 at 11:52 pm #

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

  17. sairiurse November 15, 2008 at 9:32 am #

    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

  18. HSCharles November 16, 2008 at 7:11 am #

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

  19. a December 9, 2008 at 5:39 am #


    is there a way to do select all with for example ?

  20. iTashaLIL December 10, 2008 at 5:09 am #

    Hellow

    Could you help me. I have troubles with your site. It it works not fast.
    Can somebody help me to solve the problem or explain why it can be so?

    Thanks

  21. QuicMicaPrurb December 28, 2008 at 7:37 am #

    Hi, just saying a blated hello ;)

  22. Job Abijah December 30, 2008 at 12:01 am #

    This is very handy indeed – thank you very much for posting.

    @Pidgeon – I just tested it in opera and it worked fine for me (in addition to IE, Safari & Firefox)

    Anyway thanks very much for posting this, very useful cheers.

  23. ertarunz7 January 17, 2009 at 10:31 am #

    Hii … Thanks for the script … But sorry its not working for me … can any one place an example for this .

    Thanks

  24. ertarunz7@gmail.com January 17, 2009 at 11:58 am #

    I have found one more similar scripts and it worked fine for me so i thought posting it here .. so that it may help you all.. but its working only in IE.
    Please see if any one get one that works for all (firefox, IE … )

    function ClipBoard()
    {
    //holdtext.innerText = copytext.innerText;
    Copied = holdtext.createTextRange();
    Copied.execCommand(“RemoveFormat”);
    Copied.execCommand(“Copy”);
    }

    // Html part

    hello all

    Copy to Clipboard

    Thanks

  25. ertarunz7@gmail.com January 17, 2009 at 11:59 am #

    Re-writting HTML part .. please remove quotes ..

    <!– hello all

    Copy to Clipboard –>

  26. hireecdorrozy January 22, 2009 at 2:39 am #

    Using internet is simple as hell. But I can tell y ou right now, it can be very hard, if you are the first time user.
    So, first thing I suggest – open the Explorer, and type in the address you like.
    You’ll get there really fast, it depends on your connection speed.
    Good luck.

  27. AlexStivenson February 15, 2009 at 7:12 am #

    How you think, in our situation whis crisis its actual?

  28. Sandro February 23, 2009 at 3:24 am #

    I have great difficulty understanding why some programming experts waste time by displaying outdated scripts that only function in IE.
    The whole purpose of the WWW is to produce platform, and browser independent code that everyone can use.
    I can recommend the w3c org where much discussion centres around ONE single standard.

  29. brian May 5, 2009 at 9:32 pm #

    i’ve been looking for this all over, thanks for the great post.

  30. prafuldass May 23, 2009 at 7:54 am #

    thanks for the post

  31. daya May 28, 2009 at 7:43 pm #

    great soluation.
    thanks

    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.

  32. kraloyun June 16, 2009 at 5:53 pm #

    Is there any other code shorter than this – A one line text which will select & copy the clicked area ?

  33. Greg Landers August 4, 2009 at 1:50 am #

    Add this to a textarea or text input so that when click anywhere on the field, it automatically selects all and copy’s to the clipboard.

    onclick=”this.select(); var r= this.createTextRange(); r.execCommand(‘copy’);”

  34. slashlu November 13, 2009 at 5:56 am #

    thanks for your code,it’s help for me

  35. feclaisee December 18, 2009 at 12:30 pm #

    Hi.
    My PC worked not correctly, too much errors. Please, help me to fix errors on my PC.
    My operation system is Win Vista.
    Thanks,
    feclaisee

  36. sycleordess March 22, 2010 at 12:40 am #

    Hi.
    My PC worked slowly, too much mistakes and buggs. Help me, please to fix errors on my PC. On Tipe http://www.yahoo.com/ please.
    I used Windows XP.
    Thanks,
    sycleordess

  37. Kevin March 23, 2010 at 7:32 pm #

    Wow. This is horrible advice.

    This code is not only invalid, it can break your site if you’re not careful.

    Do not use this.

    Shame on you for posting this as a tutorial.

    eliminate the heavy script and add this to your input tag:
    onfocus=”this.select();”

  38. Jonh April 8, 2010 at 5:32 am #

    I like it.

  39. A11ist3r May 4, 2010 at 5:28 pm #

    Much easier for MSIE

    clipboardData.setData(“Text”, document.getElementById( targetCopyElement ).innerHTML);

    In which targetCopyElement is ofcourse the element’s id

  40. FXbee May 16, 2010 at 3:24 am #

    Sorry. Does someone understand how to define a smart EA from bad and a clear Forex trading signals from fake? big thanks

  41. AssangeKet January 14, 2011 at 9:03 pm #

    I need to contact site admin urgently. Can you understand me?
    Hih you hear me??
    bye bye ;) )

  42. Maxnumar March 2, 2011 at 10:12 pm #

    Hellow Glad to see you my friends!) I’m Max from LA

  43. cedissuef June 12, 2011 at 3:41 am #

    hi there amazing thread you have going here.

  44. cedissuef June 13, 2011 at 1:54 am #

    hi there amazing topic we have going here!

  45. abac November 7, 2011 at 3:32 pm #

    great job. very nice. thanks

Leave a Reply

More in html (29 of 33 articles)


Yslow >>> Why my website is slow ?? Yslow is an addon for mozilla integratted with firebug (Another addon ...