Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <button>Add a child</button>
  <button>Remove a child</button>
  <my-elem></my-elem>
  <script>
    customElements.define('my-elem', class extends HTMLElement {
      constructor() {
        super();
        this.attachShadow({mode: 'open'});
        this.shadowRoot.innerHTML = `<slot></slot>`;
        this.shadowRoot.children[0].addEventListener('slotchange', _ => console.log('fire'));
      }
    });
    const m = document.querySelector('my-elem');
    document.querySelector('button:nth-child(1)').onclick = _ => {
      const div = document.createElement('div');
      m.appendChild(div);
    };
    document.querySelector('button:nth-child(2)').onclick = _ => {
      m.removeChild(m.firstChild);
    };
  </script>
</body>
</html>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers