Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
Custom HTML elements not rendering children

The following code does not render the CategoryElement's children - only the <p> element within the Shadow Root. How do I make the CategoryElement's children render?

<html>
    <head>
    </head>
    <body>
        <script>
            class CategoryElement extends HTMLElement {
                constructor() {
                    super();
                    this.attachShadow({mode: 'open'});
                    this.header = document.createElement('p');
                    this.header.innerText = 'Shadow Header';
                    this.shadowRoot.append(this.header);
                }
            }
            customElements.define('category-element', CategoryElement);

            var elem = document.createElement('category-element');
            document.body.appendChild(elem);

            var outer = document.createElement('p');
            outer.innerText = 'Outer Header';
            elem.appendChild(outer);
        </script>
    </body>
<html>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I believe this gets you there, but I'm no shadow dom expert by any stretch of the imagination.

I added .shadowRoot as shown here:

elem.shadowRoot.appendChild(outer);

When I inspect with the dev tools I can see that both <p> are in side the shadow-root element (if that's the correct way to refer to it).

</head>

<body>
  <script>
    class CategoryElement extends HTMLElement {
      constructor() {
        super();
        this.attachShadow({
          mode: 'open'
        });
        this.header = document.createElement('p');
        this.header.innerText = 'Shadow Header';
        this.shadowRoot.append(this.header);
      }
    }
    customElements.define('category-element', CategoryElement);

    var elem = document.createElement('category-element');
    document.body.appendChild(elem);

    var outer = document.createElement('p');
    outer.innerText = 'Outer Header';
    elem.shadowRoot.appendChild(outer);
  </script>
</body>
<html>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!