I've been getting a problem when trying to append an element in JavaScript
, the error I've been getting looks a bit like this:
Uncaught TypeError: Failed to execute
appendChild
onNode
: parameter 1 is not of typeNode
.
I'm also using using a framework
called Interact.js
just so you know
here's the peice of code that the browser isn't happy about:
var tempText = [];
var classNum = event.relatedTarget.getElementsByClassName('text');
var newCont = document.createElement('div');
for(var i = 0; i < classNum.length; i++){
tempText.push(event.relatedTarget.getElementsByClassName('text')[i].textContent);
}
for(var i = 0; i < tempText.length; i++){
var pText = document.createElement('p').appendChild(tempText);
newCont.appendChild(pText[i]);
}
var placement = document.getElementById('toolbar')[0];
placement.appendChild(newCont);
I just noticed a small mistake. The
document.getElementById
returns only a single object. So don't use the[0]
:But the whole thing is really easy to do using jQuery. Since you are fine with using a jQuery solution, read on. Please include the jQuery library by adding this piece:
And the JavaScript would be:
Since I am unaware of the HTML underlying, I just guessed it and converted a few to jQuery.