SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Onclick Select All and Copy to Clipboard?

    Hi I have a textarea with code text in it, and would like to have a link or button which when the user clicks on it, it will select ALL the code and copy it to the clipboard at the same time...

    Can anyone help me?

    I found this code which seems to work only in IE and it has something strange when I copy it to my HTML...link below...

    http://www.sajithmr.com/onclick-sele...rd-javascript/

  2. #2
    SitePoint Guru alecrust's Avatar
    Join Date
    Jun 2007
    Location
    London, UK
    Posts
    728
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Give this a try:
    Code:
    <html>
    <title>JavaScript: Select All</title>
    <body>
    <center>
      <form>
        <textarea name="text_area" rows="10" cols="80">
          blah blah blah blah blah blah blah 
        </textarea>
      <br>
      <input type="button" value="Highlight Text" onClick="javascript:this.form.text_area.focus();this.form.text_area.select();">
      </form>
    </center>
    </body>
    </html>
    Honda Civic Forum - UK Civic Forum
    Web Host Reviews - Independent, honest host reviews
    My Portfolio - Simplicity is the ultimate sophistication

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and to copy the text at the same when you click that button?

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only cross browser way I've heard of copying to the system clipboard is to use flash... something like call javascript function to call into movieclip function then inside that call System.setClipboard(string)...

    Firefox(Mozilla) doesn't allow this operation without enhanced privilages (editing prefs files, etc)...

    MS is proprietry... window.clipboardData.setData(...) or document.execCommand('copy', false, null)

    Opera/Safari... I'm not sure they support anything natively.

    So you're best bet is flash... hope this helps.

    Cheers,
    WF

  5. #5
    SitePoint Guru alecrust's Avatar
    Join Date
    Jun 2007
    Location
    London, UK
    Posts
    728
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you click the "Highlight Text" button it will select everything in the <textarea>.
    Honda Civic Forum - UK Civic Forum
    Web Host Reviews - Independent, honest host reviews
    My Portfolio - Simplicity is the ultimate sophistication

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @alecrust that will only select the text the only way I know of copying to the system clipboard using javascript xbrowser is via the flash method.

  7. #7
    SitePoint Guru alecrust's Avatar
    Join Date
    Jun 2007
    Location
    London, UK
    Posts
    728
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My fault sorry I thought that was all that was required.
    Honda Civic Forum - UK Civic Forum
    Web Host Reviews - Independent, honest host reviews
    My Portfolio - Simplicity is the ultimate sophistication

  8. #8
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If no one else has an idea how to do it...can you refer to the flash thing to do this?

  9. #9
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you google for "flash javascript clipboard" there's quite a few solutions people have written, so you should be able to pick one without too must hassle (I think your users will require flash 7+ player)...

    I've not had to implement this technique yet myself (but there is a project I'm working on that will require this functionality), so all I can do at the mo is point you in a direction

    Hope this helps.

    WF


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •