Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Narrow down CSS selector to data-option-id

With the code below, I’m hiding a product option from a Shopify app. I need to narrow down the CSS selector to a data-option-id attribute so that only that product option is hidden in case I add another option to the page.

{% if template contains "product" and product.handle == "copy-2-add-a-custom-logo" %}
  if (getCookie(customLogoCookie) == "1") {
    let myInterval = setInterval(function() {
      const customLogoOptionSetId = "gsAppContainer";  // Fixed id
      let customLogoSelector = document.getElementById(customLogoOptionSetId);
      if (!customLogoSelector) {
        return;
      }

      clearInterval(myInterval);

      // Hide the custom options. We need to go 3 levels up.
      customLogoSelector.style.display = "none";

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use the attribute selector when you want to hide div your arrow points to.

[data-option-id="yourIDhere"] {
  display: none;
}

When you want to hide the parent id="gsAppContainer" you have to either work in your template or via JavaScript, as there are no CSS selectors to style parents in CSS 3.

const childElement = document.querySelector('[data-option-id="yourIDhere"]');
childElement.closest('#gsAppContainer').style.display = "none";

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading