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.
- button.innerText = pokemonRepository.name should be pokemon.name
- 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
How to Make Div Fixed After You Scroll to That Div
Get Unique Selector of Element in Jquery
Jquery Submit Form and Then Show Results in an Existing Div
How to Determine Which Submit Button Was Pressed, Form Onsubmit Event, Without Jquery
Scope of Sessionstorage and Localstorage
How to Open a New Window and Insert HTML into It Using Jquery
How to Sort Elements by Numerical Value of Data Attribute
How to Manually Trigger Validation with Jquery Validate
JavaScript Onclick Event Over Flash Object
Keycode on Android Is Always 229
How to Get a HTML Element from a String with Jquery
How to Access the Contents of an Svg File in an <Img> Element
How to Get the HTML for a Dom Element in JavaScript
How to Maintain a Websockets Connection Between Pages
Sharing a Variable Between Multiple HTML Pages