The first thing to remember is that shadowDOM encapsulates DOM and CSS and NOT JavaScript.
The second thing to know is that you can not introduce script into the DOM by using .innerHTML
. See this link. Instead the class that defines your component is how to encapsulate your JS code.
While you can't use .innerHTML
you can create a script element and set its .textContent
and then call .appendChild()
for that.
const script = document.createElement('script');
script.textContent = `alert("hey");`;
const el = document.querySelector('.testing');
el.attachShadow({mode:'open'});
el.shadowRoot.appendChild(script);
<div class="testing"></div>
But, given #2 this probably is not what you really want to do.