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!
Asked
Active
Viewed 96 times
-1
-
Help with homework is off topic. – speciesUnknown Dec 27 '19 at 16:25
-
1Selecting elements is like the first lesson in any tutorial. Please put in minimal effort. – Shilly Dec 27 '19 at 16:27
-
$('.someClass').is(':visible') – Evik Ghazarian Dec 27 '19 at 16:27
-
@gburton Help with homework is on topic. [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822/215552). – Heretic Monkey Dec 27 '19 at 16:27
-
Does this answer your question? [How to detect element being added/removed from dom element?](https://stackoverflow.com/questions/20156453/how-to-detect-element-being-added-removed-from-dom-element) – Heretic Monkey Dec 27 '19 at 16:28
-
1Can you add if you tried any code? – ariferol01 Dec 27 '19 at 16:28
1 Answers
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