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

102
Views
Nested HTML element not assigning to slot

In the below code I am trying to assign <span slot='test-slot'>b</span> to <slot name='test-slot'>a</slot> but the assignment does not work. If I bring <span slot='test-slot'>b</span> outside of its parent <div> container the assignment does take place as expected.

Why is this? Is there anyway you can assign from nested elements with the slot element? If not, any alternatives? This is obviously a reduced test case but in my real web component, it is much more intuitive for a user to add an element with the slot tag within other containers.

<test-element>
  <div>
    <span slot='test-slot'>b</span>
  </div>
</test-element>

<template id='template-test-element'>
    <slot name='test-slot'>non slotted content</slot>
</template>

<script>
  class TestElement extends HTMLElement {
    constructor() {
      let template = document.getElementById("template-test-element")
                             .content.cloneNode(true);
      // Initialise shadow root and attach table template
      super() // sets AND return 'this' scope
        .attachShadow({mode:"open"}) // sets AND returns shadowRoot
        .append(template);
    }
  }

  customElements.define('test-element', TestElement);
</script>

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

0

Named slots only accept top-level children that have a matching slot-attribute.

See this (old) Polymer explainer or this more recent article.

Edit: Not sure where this is coming from though, the spec fails to mention this requirement: https://html.spec.whatwg.org/multipage/dom.html#attr-slot

It also is neither mentioned here nor here.

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!