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

230
Views
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 answers
Answer question

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 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!