Simply add the attribute
data-clipboard-targetto an action button with the input element's
idto get it working. Then add the clipboard JS to initialize it.
// Select elementsconst target = document.getElementById('kt_clipboard_2');const button = target.nextElementSibling;// Init clipboard -- for more info, please read the offical documentation: https://clipboardjs.com/var clipboard =newClipboardJS(button,{
target: target,text:function(){return target.innerText;}});// Success action handler
clipboard.on('success',function(e){const currentLabel = button.innerHTML;// Exit label update when already in progressif(button.innerHTML ==='Copied!'){return;}// Update button label
button.innerHTML ='Copied!';// Revert button label after 3 secondssetTimeout(function(){
button.innerHTML = currentLabel;},3000)});
<divclass="card card-bordered"><divclass="card-body"><!--begin::Input--><textareaid="kt_clipboard_2"class="form-control mb-3">This is an example to cut with Clipboard</textarea><!--end::Input--><!--begin::Button--><buttonclass="btn btn-light-primary"data-clipboard-action="cut"data-clipboard-target="#kt_clipboard_2">
Cut
</button><!--end::Button--></div></div>
Predefined Value
Add the
data-clipboard-textto an action button and it will copy the value set on click. Then add the clipboard JS to initialize it.
// Select elementconst target = document.getElementById('kt_clipboard_3');// Init clipboard -- for more info, please read the offical documentation: https://clipboardjs.com/
clipboard =newClipboardJS(target);// Success action handler
clipboard.on('success',function(e){const currentLabel = target.innerHTML;// Exit label update when already in progressif(target.innerHTML ==='Copied!'){return;}// Update button label
target.innerHTML ='Copied!';// Revert button label after 3 secondssetTimeout(function(){
target.innerHTML = currentLabel;},3000)});
<divclass="card card-bordered"><divclass="card-body"><!--begin::Button--><buttonclass="btn btn-light-primary"data-clipboard-text="Just because you can doesn't mean you should — clipboard.js">
Copy to clipboard
</button><!--end::Button--></div></div>
Static Element
Add the attribute
data-clipboard-targetto an action button with the static element's
idto get it working. Then add the clipboard JS to initialize it.
<divclass="card card-bordered"><divclass="card-body"><!--begin::Row--><divclass="d-flex align-items-center flex-wrap"><!--begin::Input--><divid="kt_clipboard_4"class="me-5">This is a sample static text string to copy !</div><!--end::Input--><!--begin::Button--><buttonclass="btn btn-icon btn-sm btn-light"data-clipboard-target="#kt_clipboard_4"><!--begin::Svg Icon | path: icons/duotune/general/gen054.svg--></button><!--end::Button--></div><!--end::Row--></div></div>