I just started to learn Javascript, and I’m trying to append anchor tag inside the li tag.
JavaScript
x
5
1
const li = document.createElement("li")
2
const Anchor = document.createElement("a")
3
Anchor.href = "index.html"
4
li.appendChild(Anchor)
5
This was how I did it. But when I run this,
“Failed to execute ‘appendChild’ on ‘Node’: Nodes of type ‘a’ may not be inserted inside nodes of type ‘LI’.”
this error came out. How can I fix it?
Advertisement
Answer
first you need to create
an element (a
)
JavaScript
1
13
13
1
const li = document.createElement("li")
2
const Anchor = document.createElement("a")
3
4
// SET attribute
5
// Anchor.href = "index.html"
6
// OR
7
const linkHref = document.createAttribute("href");
8
linkHref.value = "index.html";
9
Anchor.setAttributeNode(linkHref);
10
11
12
li.appendChild(Anchor)
13