That\'s it :) I have a div with the id #toCopy, and a button with the id #copy. What\'s the best way to copy #toCopy conte
You can copy to clipboard almost in any browser from input elements only (elements that has But I use this trick to do so: Little late but hope that helps!.value property), but you can't from elements like , ... (elements that has .innerHTML property).
innerHTML from
.value of to clipboard element we just createdfunction CopyToClipboard (containerid) {
// Create a new textarea element and give it id='temp_element'
var textarea = document.createElement('textarea')
textarea.id = 'temp_element'
// Optional step to make less noise on the page, if any!
textarea.style.height = 0
// Now append it to your page somewhere, I chose
document.body.appendChild(textarea)
// Give our textarea a value of whatever inside the div of id=containerid
textarea.value = document.getElementById(containerid).innerText
// Now copy whatever inside the textarea to clipboard
var selector = document.querySelector('#temp_element')
selector.select()
document.execCommand('copy')
// Remove the textarea
document.body.removeChild(textarea)
}