0

I am playing around with DOM commands to get a better understanding of them. However, I am getting an error message when I try to append a string of HTML to a child of an element I've selected. I get an error message that says my div3 variable is not a node. However, when I console.log(div3) it is listed as a node in my console log. What am I doing wrong?

var div3 = document.getElementById('div3');
div3.innerHTML = "Test String";
console.log(div3);

div3.appendChild("<h2>myH2 tag</h2>")
<div id="div3"><p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Consequatur, natus.</p></div>

4
  • 2
    It's not about div3, it's about the fact that you're passing a string, as opposed to a node to appendChild() Commented Jul 23, 2020 at 13:58
  • 2
    <h2>myH2 tag</h2> is not a Node, it's a string/text. You can read up on the topic here Commented Jul 23, 2020 at 13:58
  • @MarcHjorth Please don't link to w3schools; use official docs: developer.mozilla.org/en-US/docs/Web/API/Node/appendChild Commented Jul 23, 2020 at 13:59
  • 1
    @ChrisG Personally, I believe the W3 docs are easier to understand for newcomers than the official docs - especially this one regarding appending a child. But I will keep that in mind, thanks! Commented Jul 23, 2020 at 14:01

3 Answers 3

5

If you're getting the same error message as me, it is the following:

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

This is because "<h2>myH2 tag</h2>" is not a node.

If you want to append a tag like that you'll have to create a node from scratch, and then append it:

// your code:
var div3 = document.getElementById('div3');
div3.innerHTML = "Test String";

// new:
const newNode = document.createElement('h2')

newNode.innerText = "myH2 tag"

div3.appendChild(newNode)

That should do it.

Sign up to request clarification or add additional context in comments.

Comments

1

You need convert div3 variable for DomElement use this: div3 = new DOMParser().parseFromString(div3, "text/xml"); before call appendChild()

Comments

1

The reason is that youo're trying to pass a string to the appendChild function - it needs to be of Node Type. You could pass the tags inside of innerHTML instead along with the content but the snippet I added gives you an idea of how to go abut it.

MDN link for appendChild - MDN is our friend: https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild

let parent = document.querySelector('.my-body');
let header = document.createElement('h2');
// insert content into the header we created above
header.innerHTML = 'This is some heading text';
// append header to the container
parent.appendChild(header);
<div class="my-body"></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.