Search code examples
polymerweb-componentpolymer-2.x

Polymer 2.0 : How to dynamically append child in shadow dom, as append child doesn't work


Getting this error message when try to add a child element :

Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.


Solution

  • You can try that :

          // Get element from shadow dom
          var containerEle = Polymer.dom(this.root).querySelector('shadow_dom_selector');
    
          // Create dynamic element
          var newEle = document.createElement('span');
          newEle.textContent = 'Hello World';
    
          // Append
          containerEle.appendChild(newEle);
    

    If your container element have an id

          Polymer.dom(this.$.containerElementId).appendChild(newEle);