document.querySelector("#myBtn").onclick = function () {
console.log("Clicked");
const h1 = document.querySelector("#myH1");
const underline = document.createElement("u");
underline.innerText = "Hey bro.";
h1.innerText = "";
h1.append(underline);
const p = document.querySelector(".myP");
p.forEach((element) => (element.innerText = "Thank You!!!!"));
const btn = document.querySelector("button");
btn.remove();
};
p.forEach((element) => (element.innerText = "Thank You!"));
I got: Error TypeError p.forEach is not a function
querySelector returns just 1 element. Use querySelectorAll to return a list of elements matching the given selector.
The Document method querySelector() returns the first Element within the document that matches the specified selector.
You could use the getElementsByClassName instead
var p = document.getElementsByClassName('myP"');
for (let i = 0; i < p.length; i++){
p[i].innerText = "Thank You!!!!";
}
First , Check you createated a query or not.Then variable p is not defined inside the function.use query selector all for return all the elements in the query.