-2

I want to hide a text:

<strong class="showrelated">In Verbindung stehende Artikel: </strong>
<?php echo do_shortcode('[custom-related-posts title="" order_by="title" order="ASC" none_text=""]'); ?>

If css class is not declared. My shortcode function get this class .crp-list

Not sure how can I do that. I have tried the following but nothing happens:

if ($(".crp-list")[0]) {
    $(".showrelated").show();
} else {
    $(".showrelated").css("display","none");
}
Krzysztof Janiszewski
  • 3,763
  • 3
  • 18
  • 38

2 Answers2

0

When crp-list class is present in a DOM.

if ($(".crp-list").length !== 0) {
  $(".showrelated").show();
} else {
  $(".showrelated").hide();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<strong class="showrelated">In Verbindung stehende Artikel: </strong>

<div class="crp-list"></div>

When crp-list class is not present in a DOM.

if ($(".crp-list").length !== 0) {
  $(".showrelated").show();
} else {
  $(".showrelated").hide();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<strong class="showrelated">In Verbindung stehende Artikel: </strong>
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
0

Another option would be to check if the element you are talking about has a specific class but this is a guess because I don't understand your question entirely.

if ( $('#element-to-check').hasClass('crp-list') ) {
    $(".showrelated").show();
}
Martin Geldart
  • 407
  • 2
  • 8
  • This is does not solve the problem. Probably the element with class `crp-list` does not have an id and then your solution does not work. – Krzysztof Janiszewski Aug 21 '18 at 13:14
  • 2
    @KrzysztofJaniszewski - thanks, but at the time of posting, the OP didn't share the code that showed the element that may or may not have an ID, and therefore I took a guess, as I clearly stated in my answer! I suggest you be pedantic elsewhere, as I usually post decent answers where a poster gives decent information. And I don't suppose the OP has an element called element-to-check, either!!! – Martin Geldart Aug 21 '18 at 15:25
  • Answers do not have to be 100% working solutions. It is perfectly okay to give an example or a demonstration of how something works for the OP to take away and try. – Martin Geldart Aug 21 '18 at 15:32