Reputation:
I don't know how to copy the value to clipboard from the input without using ID!
Here is JS:
new Clipboard(".form__copy-btn", {
target: function (trigger) {
return $(trigger).closest(".form__field-wrapper").find("input");
}
});
Upvotes: 2
Views: 984
Reputation:
Also, I found another answer, here it is:
new Clipboard(".form__copy-btn", {
target: function (trigger) {
return $(trigger).closest(".form__field-wrapper").find("input").get(0);
}
});
Upvotes: 0
Reputation: 391
I have checked your codepen. I have made below changes and copy is working
Remove clipboard js plugin code from js and add itin setting as external resource resource in js section (external resource cdn link: https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.7.1/clipboard.min.js)
I have made below change in your code
/** I have used text method for copy and it is working **/
var clipboard = new Clipboard(".form__copy-btn", {
text: function(trigger) {
// console.log($(trigger).closest(".form__field-wrapper").find("input").val());
return $(trigger).closest(".form__field-wrapper").find("input").val();
}
});
Upvotes: 0
Reputation: 8650
Two things are happening here:
jQuery doesn't work withing the clipboard return function. I passed it using var j = $;
You are trying to return a jQuery element and clipboard.js only takes a native JS element. Instead of using target:
use text:
- that way you can pass the exact text string instead of the element.
This worked for me:
var j = $;
_document.ready(function () {
new Clipboard(".form__copy-btn", {
text: function(trigger) {
return j(trigger).closest(".form__field-wrapper").find("input").val();
}
});
...
Upvotes: 2