skip to Main Content

Here is my code i mixed up liquid code and js i got product handle but this if condition is not working i have tag with attribute value still it shows not found can someone help me with this issue

<a class="hidden_wrap" name="hidden_link" data-color="gold" data-stone="tiger-eye" data-handle="copy-of-joory-earrings-tiger-eye-gold
" style="appearance: auto;"> gold-tiger-eye   </a>

<script>
          var handle = "{{product.handle}}";
         if ($(`a[class='hidden_wrap'][data-handle='${handle}').length > 0) {
                    alert("found");
                  }else
                  {

              alert('not found');
          }
</script>
            

2

Answers


  1. Try With This,
    Use backtick for product.handle and also for html content in jquery. 🙂

    & Remove White Space after copy-of-joory-earrings-tiger-eye-gold.

    <script>
         var handle = `{{product.handle}}`;
         if ($(`a[class='hidden_wrap'][data-handle=${handle}`).length > 0) {
              alert("found");
         }
         else{
              alert('not found');
         }
    </script>
    
    Login or Signup to reply.
  2. Try this
    You can directly get the value by attribute using jquery.

    <script>
    var handleAttr = $(".hidden_wrap").attr("data-handle");
    if(handleAttr){
    alert("found");
    }else{
    alert("not found");
    }
    </script>
    

    Thanks.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search