-1

How to track the presence of DIV on page? If there is DIV on page, script catch it (console.log("there is DIV on page"), If there is NO DIV on page, script also catch it. The presence of DIV can be changed by user!

Dark Light
  • 933
  • 1
  • 10
  • 20

1 Answers1

1

getElementsByTagNames gets all divs present on the page.

It returns an array of HTML collection. If the length of the array is greater than 0, we know divs are present on the page.

Below code would console log "div is present" when atleast when div is present on the page.

let div = document.getElementsByTagName("div")
if(div.length>0)
 console.log("div is present")
kooskoos
  • 4,622
  • 1
  • 12
  • 29
  • Code-only answers are generally frowned upon on this site. Could you please edit your answer to include some comments or explanation of your code? Explanations should answer questions like: What does it do? How does it do it? Where does it go? How does it solve OP's problem? See: [How to anwser](https://stackoverflow.com/help/how-to-answer). Thanks! – Eduardo Baitello Dec 27 '19 at 17:40
  • Thanks for letting me know. I have edited my answer and added explanation:) – kooskoos Dec 27 '19 at 18:08