HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/veltech.komma.pro/resources/js/kms/attributes/copyText.js
/**
 * Give this class an htmlElement with a data-to-copy attribute,
 * and it will copy the text in the value of the attribute to your clipboard
 * when you click on it.
 */
class CopyText {
    constructor(element) {
        this.element = element;
        if(!'toCopy' in this.element.dataset) {
            console.error('CopyText: The given element must have a data-to-copy attribute with the value that you want to copy. But did not have that attribute.', element);
        }

        this.addEventListeners();
    }

    addEventListeners() {
        this.element.addEventListener('click', this.clickHandler.bind(this))
    }

    clickHandler(mouseEvent) {
        //Prevent the event from following a button if element is a button
        mouseEvent.preventDefault();

        //Get the text to copy
        let text = this.element.dataset.toCopy;

        //Temporary create an input element
        let input = document.createElement('input');
        document.body.appendChild(input);
        input.value = text;
        input.select();
        document.execCommand("copy");
        document.body.removeChild(input);
        input = null;
    }
}