1

On the product page I have this element

<span class="product-review-rating"></span>

which represent existing product ratings

and I have this one

<div class="product-detail-trustedshops-reviews" id="ts_product_widget_position" style="clear: both; margin-bottom: 1rem;"> </div>

which I want to remove if first exist

I tried with javascript like this

if (document.contains(document.getElementsByClassName("product-review-rating"))) {
            document.getElementsByClassName("product-detail-trustedshops-reviews").remove();
}   else {
        empty();
}

And I get this error in console

Uncaught TypeError: Failed to execute 'contains' on 'Node': parameter 1 is not of type 'Node'. at :1:14

If is possible I can write in jquery too.

alpham8
  • 1,314
  • 2
  • 14
  • 32
Zagor
  • 13
  • 2
  • Does this answer your question? [Is there an "exists" function for jQuery?](https://stackoverflow.com/questions/31044/is-there-an-exists-function-for-jquery) – freedomn-m Feb 09 '21 at 12:46
  • `if (document.getElementsByClassName("product-review-rating").length !==0)` – freedomn-m Feb 09 '21 at 12:46
  • Note that `.remove()` will is also only available on a single node at a time, so you'd need to loop through your results. Or just use jquery. – freedomn-m Feb 09 '21 at 12:48

3 Answers3

1

Use querySelector to get the first element that matches the selector

For Example:

if (document.contains(document.querySelector(".product-review-rating"))) {
  document.querySelector(".product-detail-trustedshops-reviews").remove();
} else {
  empty();
}
firatozcevahir
  • 892
  • 4
  • 13
0

Using jQuery :

$( document ).ready(function() {
    var a = $('.product-review-rating').length;
    if(a!=0){
    $(".product-detail-trustedshops-reviews").remove();
    }
});
Sushant Bassi
  • 364
  • 3
  • 16
0

I think you entered in a JavaScript timing problem, since Trusted Shops is an external script, which is loaded later than the web packed' Shopware part.

So, the better approach would be to hide it via CSS.

Or even much better: Check the plugin settings, usually you can hide such stuff in the Trusted Shops Shopware Plugin implementation itself.

alpham8
  • 1,314
  • 2
  • 14
  • 32