appendChild to non empty div in lightning web component

I am using a thrid party library to render certain elements in a ‘container’, and as per the documentation on lwc directives, I understand that I have to add a directive to specificaly indicate that the dom can me targeted by
appendChild, however, when I try to add the directive to a non-empty element and deploy it, I get the following error:

“error”:”LWC1086: Invalid contents for element with \”lwc:dom\”.
Element must be empty”

I have other embeded elements that are also used by the frameowrk (to do the same)

If i do not add the directive, I run into this error:

Error: [LWC error]: appendChild is disallowed in Element unless lwc:dom="manual" directive is used in the template.

this seems counterproductive =/ and i’m assuming there is no workaround ? (or is there?)

Not sure this is helpful, but here is an example snippet:

    <div class="slds-m-around_medium">
        <div id="someId" class="myInterface" > <!--lwc:dom="manual" Wont work -->
            <div class="tab-section">
                <a class="A-Tab" data-id="All" data-caption="All Content" lwc:dom="manual"></a>

            <div class="main-section">
              <div class="results-column">

                <div class="resultList" data-layout="list" lwc:dom="manual">

                <div class="pager" lwc:dom="manual"></div>
                <div class="resultsPerPage" lwc:dom="manual"></div>

.js file

--in my default class class that extends the LightningElement--
    renderedCallback() {
        if (this.framewokrInitialized ) {
        this.framewokrInitialized = true;
        // initialize component
            loadScript(this, ResourcesC + '/js/resource1.min.js'),
            loadStyle(this, ResourcesC + '/css/resource2.css'),
            loadScript(this, ResourcesC + '/js/resource3.js')

        ]).then(() => {
            }).catch(error => {
                //do something


            Framework.aClass.aMethod('args', 'more args')
            const root = this.template.querySelector(".myInterface"); 


The lwc:dom="manual" directive indicates to the engine that it should not touch the child nodes of a specific element. Because of this, the LWC compiler will complain if you add children elements to this.

The only way to add children node to an element with lwc:dom="manual" directive is via javascript:

  <div class="container" lwc:dom="manual"></div>
import { LightningElement } from 'lwc';

// The content you want to insert in container
const CONTAINER_HTML = `<div>Some HTML</div>`;

export default class extends LightningElement {
  renderCallback() {
    const container = this.template.querySelector('.container');
    container.innerHTML = CONTAINER_HTML;
    // ... Do some logic with the container ...

Source : Link , Question Author : glls , Answer Author : pmdartus

Leave a Comment