Apologies if this isn't the right place for this, I'm new to Github.
When my user clicks a button I run some javascript that generates a string. I'd like to have that string copied to the clipboard. The problem is that at this point the string only exists in a variable and not in an input or data- attribute. I can't seem to figure out if clipboard.js can copy to the clipboard straight from a variable when the string doesn't exist in the document. I suppose I could apply the string to an element first and then copy it to the clipboard, but as I have no use for the string in the document I'd like to skip that and just send it straight to the clipboard when the user clicks a button.
Yes, as seen in the docs:
var copyThis = 'foobar';
new Clipboard('.btn', {
text: function(trigger) {
return copyThis;
}
});
Thank you @codeclown I've also needed this question answered, but being new to Javascript I found it difficult to understand the syntax of your answer in the docs and therefore hard to implement. Is there somewhere I could go to understand this better? (I don't want to waste any more of your time in asking for an explaination)
Thank you! One last question, is it possible to do this same thing without requiring the user to click an element?
Unfortunately no @jimmykup, user interaction is required.
@Adomeister here's a good resource to learn JavaScript: http://bonsaiden.github.io/JavaScript-Garden/
thanks @zenorocha I've only been dabbling for a while but now I would like to make an effort at upgrading my limited knowledge and exposure.
@codeclown
var copyThis = 'foobar'; new Clipboard('.btn', { text: function(trigger) { return copyThis; } });
Hmm . . . That Doesn't Seem To Work Now.
Sure I Changed It To new ClipboardJS(), But IDK Why ?
Most helpful comment
Yes, as seen in the docs: