-1

I have some code that creates a new paragraph element within a div and gives it the class "taskparagraph". I want to check if any elements with this class exist, and if not, add another (unrelated) paragraph.

I genuinely have no clue how to do something like this even though it seems like such a simple task. Maybe I'm just not Googling the right things...

P.S. I do not plan on using any JS libraries like jQuery for now. If using something like that is the only possible way to do this task please let me know.

asterisk
  • 1
  • 1

1 Answers1

1

You can use document.querySelector:

if (!document.querySelector('.taskparagraph')) { // or compare with null
    // no element with class "taskparagraph" exists
}
Unmitigated
  • 76,500
  • 11
  • 62
  • 80