Question

I have a list of textarea fields on the page. I need to be able to click on any of them and copy just the content of the clicked on into the clipboard. Since there are many, I prefer to refer to them as their textarea element instead of giving an ID.

I did come to the following code which works but I notice that is not very responsive. The official zClip page suggests to load the zclip when the page loads but I do not how to do so and still work for any given textarea element.

$(document).ready(function() {
  return $("textarea").click(function() {
    return $("textarea").zclip({
      path: "/assets/ZeroClipboard.swf",
      copy: $(this).text(),
      afterCopy: function() {
        return $(this).select();
      }
    });
  });
});

Any idea how to do so?

Was it helpful?

Solution

I think your code should just read

$(document).ready(function() {
    $("textarea").zclip({
        path: "/assets/ZeroClipboard.swf",
        copy: function () {
            return $(this).val();
        },
        afterCopy: function() {
           $(this).select();
        }
    });
});
  • Why would you call zclip everytime you click on a textarea?
  • No need for extraneous returns, the only one that may be doing something is the click one, except that there is no default action for clicking on a textarea
  • The copy config option should use a function so it can return the current value of the textarea, not the initial value

The examples on the page show applying zclip to another element, it will copy when you click that element, not the textarea, you'll have to dig into that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top