Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

222
Visualizações
How to move children to a new parent, execpt the last child? (JavaScript/jQuery)

I have a bit of JS code, which creates a new, custom HTML element. I need to move the child elements to the new parent, which is inside the custom element (I need two elements because of styling), but as elements put inside the tags are created first, I need to have all children of moved to the last child of (I already create the new parent automatically). Note: jQuery solutions are acceptable.

Code:

    connectedCallback() {
        this.setAttribute('class', 'e1')
        const e1 = document.createElement('div')
        e1.setAttribute('class', 'e-content')
        this.appendChild(e1)
   }

class ce extends HTMLElement {
    constructor() {
        super();
    }
    connectedCallback() {
        this.setAttribute('class', 'e1')
        const e1 = document.createElement('div')
        e1.setAttribute('class', 'e-content')
        this.appendChild(e1)
    }
}
customElements.define('custom-elem', ce);
<custom-elem>
    <p>A child of custom-elem, which should go in the generated child of custom-elem (div with class e-content).</p>
</custom-elem>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Looks like adding two statements (could likely be reduced to one) to the end of the connectedCallback function should cover it:

class ce extends HTMLElement {
    constructor() {
        super();
    }
    connectedCallback() {
        this.setAttribute('class', 'e1')
        const e1 = document.createElement('div')
        e1.setAttribute('class', 'e-content')
        this.appendChild(e1)
        var pElement = this.getElementsByTagName('p')
        e1.appendChild(pElement[0])        
    }
}
customElements.define('custom-elem', ce);
<custom-elem>
    <p>A child of custom-elem, which should go in the generated child of custom-elem (div with class e-content).</p>
</custom-elem>

Use the browser's inspector to see that the <p> element is moved to inside the <div class="e-content"> element.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda