Search code examples
javascriptprototypeappendchild

Getting the error: Failed to execute 'appendChild' on 'Node'


Am using pure javascript prototype functions to create a list of checkbox and append them as we iterate over the data. Everything is created using javascript and css. When I run it so it can draw a list of checkboxes, it throws me an error :

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

for this line:

results.push(this.list.appendChild(letter.draw));

Am sure there is nothing wrong with checkboxinput prototype since it works fine everywhere else. There is something wrong am doing in FormTicket.Can anyone help me what am doing wrong? The form page is FormTicket

FormTicket.prototype.respond = function(data) {
  var letter, i, len;
  this.letters = [];
  for (i = 0, len = data.length; i < len; i++) {
    letter = data[i];
    this.letters.push(new ParaInfo({
      InfoIndividual: letter
    }));
  }
  return this.drawList();
};

FormTicket.prototype.drawList = function() {
  var letter, i, len, ref, results;
  ref = this.letters;
  results = [];
  for (i = 0, len = ref.length; i < len; i++) {
    letter = ref[i];
    results.push(this.list.appendChild(letter.draw));
  }
  return results;
};

The draw function in the ParaInfo method is:

 ParaInfo.prototype.draw = function() {
  this.e = new CheckBoxInput({
    title: this.InfoIndividual,
    float: 'left',
  });
  return this.e;
};

and just FYI, the checkboxinput is another prototype, part of it as below:

CheckBoxInput.prototype.build = function(arg) {
  this.title = arg.title;
  this.float = arg.float;
};

CheckBoxInput.prototype.draw = function() {
  var box, check;
  this.item = document.createElement('div').addClass('checkboxInput');
  return this.item;
};

Solution

  • Solved it. needed to call ".draw().draw()" i.e Initially was calling .draw() of ParaInfo only. Added another ".draw()" which calls the draw of CheckBoxInput. Correct call for append:

      results.push(this.list.appendChild(letter.draw().draw()));
    

    Thanks to user apsillers for his valuable input as well.