Error: Failed to Execute 'Appendchild' on 'Node': Parameter 1 Is Not of Type 'Node'

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

Your function is returning a string rather than the div node. appendChild can only append a node

var d= document.createElement("div");
d.classList.add("help-block");
document.getElementById("firstName- group").appendChild(d);

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

The line variable you're passing isn't a Node, it's a String. Try first using

var line = document.createElement("p");
line.innerHTML = "<strong>" + name + ": </strong>" + message.field_message_body.und[0].value;
document.getElementById("messages").appendChild(line);

Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node' error

From what I understand you are trying to make a list of buttons, but from your code that is not what is happening. You are trying to embed a list inside a button.

<ul class="pokemon-list">

</ul>

<script>
//IIFE function
let pokemonRepository = (function () {
//List of pokemon Array
let pokemonList = [
{
name: 'Bulbasaur ',
height: 2.4,
types: ['Grass ', 'Poison']
},
{
name: ' Charmander ',
height: 2,
types: 'Fire'
},
{
name: ' Squirtle ',
height: 1.8,
types: 'Water'
}
];
//adds pokemon to the pokedex
function add(pokemon) {
pokemonList.push(pokemon);
}
function getAll() {
return pokemonList
}
return {
add: add,
getAll: getAll
}
})();
// for each function to write pokemon and its name
pokemonRepository.getAll().forEach(function(pokemon){
let listContainer = document.querySelector('.pokemon-list');
let listItem = document.createElement('li');
let button = document.createElement('button');
button.innerText = pokemon.name;
button.classList.add('poke-button');
listItem.appendChild(button);
listContainer.appendChild(listItem);
});

I spotted some few things.

  1. button.innerText = pokemonRepository.name should be pokemon.name
  2. You are appending listContainer and listItem to the button so change the followings.

button.appendChild(listContainer);
button.appendChild(listItem)

to

listItem.appendChild(button);
listContainer.appendChild(listItem);

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

You should create the text as textNode using createTextNode() method like,

const textNode = document.createTextNode(" and this is the new text added");

and pass the node created as parameter to appendChild like,

document.currentScript.parentNode.appendChild(textNode);

And the modified snippet as sollows,

<!-- Many elements above this --><p>  This a part of the text  <script>    const textNode = document.createTextNode(" and this is the new text added");    document.currentScript.parentNode.appendChild(textNode);  </script></p><!-- Many elements under this -->


Related Topics



Leave a reply



Submit