appendChild() to a DOM node not working

I am writing a JS plugin that renders a custom widget. The code structure is like:

// It is stored somewhere like $:/plugins/LittleYe233/MyPlugin/widget.js, which is a normal tiddler

function foo() {
  const node = $tw.utils.domMaker('div');
  // ...
  return node;
}

MyWidget.prototype.create = function(parent, nextSibling) {
  let container = $tw.utils.domMaker('div');
  // ...
  const node = foo();
  container.appendChild(node);
  // ...
  return container;
}

I found that container.appendChild(node) was not working as expected (appending node to container), even if it worked in the browser console. I have searched in the Internet and still can’t figure it out. Any reply will be highly appreciated.

HI LittleYe233

I usual copy the pattern of existing widgets, take a look at this one.

Thank you for your quick reply! I am too embarrassed that I just found that I overwrote container after appendChild() so that I couldn’t get the correct result. I have tested that both of your snippet and appendChild() work well.