Add Class in span attribute according to url

Asked

Viewed 84 times

0

I have a span attribute and need to insert a class to force the hide if it is within a URL, I want to do it fully in javascript.

Code I’m making

document.addEventListener('DOMContentLoaded', () => {
var classe = document.querySelector('span.price');
classe.forEach(function(classe){
  if(window.location.href == "https://www.myurl.com.br/product") {
classe.classList.add('force-hide');
  }});
});
.force-hide {
  display:none;
}
<div class="info-details"> 
<strong class="product name product-item-name"> 
<a class="product-item-link" href="https://www.myurl.com.br/product"> Product </a> 
</strong>
<div class="price-box price-final_price" data-role="priceBox" data-product-id="2293" data-price-box="product-id-2293">
<span class="price-container price-final_price tax weee"> 
<span id="product-price-2293" data-price-amount="13.9" data-price-type="finalPrice" class="price-wrapper ">
<span class="price">R$13,90</span>
</span> 
</span> 
</div></div>

I want to make sure that if it is in the given URL, it adds a force-Hide class in the span attribute.

  • But your script already works my dear. What’s the problem ai? Here testing locally the span with the price has disappeared right...

  • @hugocsl understand,are several of those on my page will be necessary to create a foreach. To add correctly in all.

  • @hucsl updated with foreach

  • That’s the idea, but is it good to keep this feature on the front end? The information will continue there, only is not shown

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.