Hide H4 without link and show H4 with link

Asked

Viewed 54 times

2

I have a div .cx-single and in it there is a <h4>, would like to display this <h4> only when it has a link. How do I do this in jQuery?

  • 1

    Link? In what sense? Anchor <a> or a URL?

  • @nicematt is a <a tag>

1 answer

3


You can do this with just one line of code using the .has() to detect whether the element h4 has a link/tag (<a>), and leave it hidden already previously with a display:none; in the CSS code as follows:

$('.cx-single h4').has('a').css('display', 'block');
.cx-single h4 a.link {
    display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="cx-single">
  <h4><a class="link">Insira o Link Aqui</a></h4>
  <h4>Insira o Link Aqui</h4>
</div>

As you can see the second link is not displayed because it contains no element with the tag <a>

Browser other questions tagged

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