material design dynamic chips with jquery - chipSetEl.appendChild is not a function

578 views Asked by At

I'm trying to add tags when a user selects an option from a dropdown. So they can pick multiple items and there would then be tags underneath demonstrating the choices.

My current attempt, trying to take from: https://material.io/develop/web/components/chips/

 <link rel="stylesheet" href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css">   
<div class="mdc-chip-set mdc-chip-set--input" role="grid"></div>
<script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js"></script>
<script type="text/javascript">
  const chipSetEl = mdc.chips.MDCChipSet.attachTo(document.querySelector('.mdc-chip-set'));
  //const chipSet = new mdc.chips.MDCChipSet(chipSetEl);

  $(function() {
    $(document).on("change", '#search_selectWrap', function() {
      const chipEl = document.createElement('div');
      // ... perform operations to properly populate/decorate chip element ...
      chipSetEl.appendChild(chipEl);
      //chipSet.addChip(chipEl);
    });
  });
</script>

My error is currently:

chipSetEl.appendChild is not a function

However if I add uncomment the chipSet initialisation, I also get the error:

this.root_.querySelectorAll is not a function

chipset el console.log:

enter image description here

EDIT

New definition of chipSetEl and attempt of customisation:

const chipSetEl = document.querySelector('.mdc-chip-set');
const chipSet = new mdc.chips.MDCChipSet(chipSetEl);
console.log(chipSetEl);

$(function()
{
    $(document).on("change", '#search_selectWrap',function ()  {
        const chipEl = document.createElement('div');
        chipEl.shouldRemoveOnTrailingIconClick = true;
        chipEl.innerHTML = "hi";
        chipEl.style["background-color"] = "green"; 
        // ... perform operations to properly populate/decorate chip element ...
        chipSetEl.appendChild(chipEl);
        chipSet.addChip(chipEl);
    });
});
0

There are 0 answers