sle7en
sle7en

Reputation: 41

check if div w/ class x exists on page w/ Jekyll Liquid

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

Answers (1)

Amir Meimari
Amir Meimari

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

Related Questions