Reputation: 41
How can I check if a div with certain class x
exists on page?
{% if page.div.class == x %}
<script></script>
{% endif %}
Would this work?
Upvotes: 0
Views: 269
Reputation: 538
With JavaScript and document.querySelector()
, you can select the first element within the document that matches your specified selector.
For example:
const targetElement = document.querySelector('.x') // .x is class you are looking for
if (targetElement) console.log('element with class of x exists')
If an element exists on your page it will return that element, otherwise null
is being returned.
Checkout MDN doc
Upvotes: 1