2

I have user reviews tab on our shop and put image to this in .tab:afterjust to make it more visible. But I want to show this content only when there already is any user review.

Why my code doesn't work?

<script>
$(document).ready(function() {
    if($('.review').length) { 
      $(".tab:after").css("display", "block");
    } 
    else { 
      $(".tab:after").css("display", "none"); 
    }
});
</script>

JSFiddle here

EDIT: Thank you all! This does the magic:

$(document).ready(function() {
    if($('.review').length >0) { 
        $(".tab").addClass('show-it')
    }
});

CSS:

.tab:after {
    display:none;
    }
.tab.show-it:after {
    display:block;
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
freewheeler
  • 1,306
  • 2
  • 12
  • 24

3 Answers3

1

Try this:

<script>
$(document).ready(function() {
    if($('.review').length >0) { 
      $(".tab:after").css("display", "block");
    } 
    else { 
      $(".tab:after").css("display", "none"); 
    }
});
</script>
Rohit Arora
  • 2,246
  • 2
  • 24
  • 40
1

That's because ".tab:after" selector will return an empty set as jQuery and JavaScript in general can't select/manipulate CSS pseudo elements. You can use a class, toggle it and leave the manipulation to CSS.

.tab.block:after {
    display: block;
    ...
}
$(".tab").toggleClass("block", $(".review").length > 0);

http://jsfiddle.net/tzefrbvr/

Ram
  • 143,282
  • 16
  • 168
  • 197
-1

You need to remove :after. See the link below

JSFiddle

Amar Kamthe
  • 2,524
  • 2
  • 15
  • 24