The point is that auto-copying works only for input or textarea elements that are highlighted with the mouse. Therefore, when you click on a button, you must βbehind the scenesβ first create such an element, paste the necessary text there, select it, copy it, then delete it:
(function(){ document.getElementById('copy').addEventListener('click', function(){ let area = document.createElement('textarea'); /* Π‘ΠΎΠ·Π΄Π°Π»ΠΈ */ area.value = document.getElementById('mama').innerHTML; /* ΠΡΡΠ°Π²ΠΈΠ»ΠΈ ΡΠ΅ΠΊΡΡ */ document.body.appendChild(area).select(); /* ΠΠΎΠ±Π°Π²ΠΈΠ»ΠΈ Π½Π° ΡΡΡΠ°Π½ΠΈΡΡ ΠΈ Π²ΡΠ΄Π΅Π»ΠΈΠ»ΠΈ */ document.execCommand('copy'); /* Π‘ΠΊΠΎΠΏΠΈΡΠΎΠ²Π°Π»ΠΈ */ area.remove(); /* Π£Π΄Π°Π»ΠΈΠ»ΠΈ */ /* ΠΠ°ΠΊΠ°Ρ-ΡΠΎ Π΄Π΅ΠΊΠΎΡΠ°ΡΠΈΡ, ΡΡΠΎΠ±Ρ Π΄Π°ΡΡ ΠΏΠΎΠ½ΡΡΡ, ΡΡΠΎ ΠΊΠΎΠΏΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅ ΡΡΠ°Π±ΠΎΡΠ°Π»ΠΎ */ this.innerHTML = 'Π‘ΠΊΠΎΠΏΠΈΡΠΎΠ²Π°<span style="color: red">Π½ΠΎ</span>'; setTimeout(function(){ document.getElementById('copy').innerHTML = 'Π‘ΠΊΠΎΠΏΠΈΡΠΎΠ²Π°ΡΡ'; },2000); }); })();
<div id="mama"> ΠΠ±ΡΠΈΠΉ Π±Π»ΠΎΠΊ, ΠΊΡΠ΄Π° Π·Π°ΠΏΠΈΡΠ°Π½ <p> ΠΊΠ°ΠΊΠΎΠΉ-ΡΠΎ HTML </p> ΠΈ Π½ΡΠΆΠ½ΠΎ Π΅Π³ΠΎ ΠΊΠΎΠΏΠΈΡΠΎΠ²Π°ΡΡ <span> mooo </span> </div> <button id="copy">Π‘ΠΊΠΎΠΏΠΈΡΠΎΠ²Π°ΡΡ</button>