Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

104
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda