DEV Community

Nischal Dahal
Nischal Dahal

Posted on

CoPY to CLIPBOARD in JavaScript !

Copy using javascript in proper way !

function copyme(e) {
    e = e || window.event;
    let target = e.target || e.srcElement;
 // const text = target.previousElementSibling.innerHTML;
        let text = e.target.value; // getting directly from input element 
    const el = 
 document.createElement('textarea');
    el.value = text;
    document.body.appendChild(el);
    el.select();
    document.execCommand('copy');
    document.body.removeChild(el);
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)