I am trying to create a font awesome icon next to the list item, but it keeps giving me [object HTMLElement]
any Idea why ?
//The icon add classes
JavaScript
x
20
20
1
const fas = document.querySelector(".fas");
2
const ul = document.querySelector("ul");
3
const input = document.querySelector("input");
4
5
//Grab the input
6
fas.addEventListener("click", () => {
7
const li = document.createElement("li");
8
const inputValue = input.value;
9
const icon = document.createElement("i");
10
icon.innerHTML = "hey";
11
li.innerHTML = inputValue + icon;
12
13
console.log(icon);
14
if (inputValue == "") {
15
return;
16
}
17
ul.appendChild(li);
18
input.value = "";
19
});
20
Thank you
Advertisement
Answer
The right way to insert your icon is not
JavaScript
1
2
1
li.innerHTML =
2
You should use li.appendChild(icon) instead. You can do the same with a textnode for your input text 🙂 Here’s my example, hopefully this is what you wanted : https://codepen.io/LENNY74/pen/dyNVVKa