I'm trying to append some text data to an Elementor div using fetch and WordPress custom REST API I made. But I'm always getting this error:
Uncaught (in promise) ReferenceError: getElementsByClassName is not defined
Here is the code:
for(var i = 0; i < document.getElementsByClassName("elementor-post__title").length; i++) {
let fetchRes = fetch("https://localhost/paperclasslk/wp-json/quiz/v1/search?name="+document.getElementsByClassName("elementor-post__title")[i].innerText);
fetchRes.then(res => res.json()).then(d => {
if (d['quiz_max_score'] === null) {
const para = document.createElement("p");
para.innerText = "Quiz " + i + " score is: No score";
document.getElementsByClassName("elementor-post__excerpt")[i].appendChild(para);
} else {
const para = document.createElement("p");
para.innerText = "Quiz " + i + " score is: " + d['quiz_max_score'];
document.getElementsByClassName("elementor-post__excerpt")[i].appendChild(para);
}
})
}
Hopefully everyone can help me. Thank you and have a good day!