0
<p class="time">
<span>1m ago</span>
<span>2 days ago</span>
</p>
<script>
let time = document.getElementsByClassName("time02");

if (time.textContent.includes("1m ago")) {
    console.log ("you have a message 1 minute before")
}
else {
    console.log ("before 1 days")
}
</script>

showing that error...

Uncaught TypeError: Cannot read properties of undefined (reading 'includes')?

  • 1
    `getElementsByClassName("time02")` returns an HTMLCollection not an individual element. Looks like you want to query the child `span`s and iterate over them. (also there are no elements with class `"time02"`) – pilchard Oct 14 '22 at 15:56
  • 1
    Does this answer your question? [What do querySelectorAll and getElementsBy\* methods return?](https://stackoverflow.com/questions/10693845/what-do-queryselectorall-and-getelementsby-methods-return) – pilchard Oct 14 '22 at 15:57
  • 2
    The key is to understand the error. "Cannot read properties of undefined (reading 'includes')" means that `time.textContent` is undefined. You can try to add a `console.log(time, time.textContent)` and maybe that will also help you see the problem – Boguz Oct 14 '22 at 16:00

0 Answers0