2

I want to hide a div container when there is no <h5> in it. Normally the div container would be hide. When the server pulls feeds from other website, a <h5> will be add inside the div container. Then the div should show up on the page.

Here is a simplify version of the HTML structure without out <h5>

<div id="alert-feed">
    <div class="webpart-title">Title</div>
    <div class="webpart-body">
        <div class="contianer">
            <ul class="feedlist">
                <li class="campusfeed">
                    <ul class="articlelist"></ul>
                </li>
            </ul>
        </div>
    </div>
</div>

Here is the HTML with <h5>

<div id="alert-feed">
    <div class="webpart-title">Title</div>
    <div class="webpart-body">
        <div class="contianer">
            <ul class="feedlist">
                <li class="campusfeed">
                    <ul class="articlelist">
                        <li>
                            <a><h5 class="articletitle"></h5></a>
                        </li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</div>

So I want to hide the #alert-feed by adding a class no-alert when there is no <h5> inside it. I know this could be simply done by using hasClass and addClass, but the structure is really confusing me and I can't do anything about it. So could someone help me to figure out how to do this?

1
  • How do you add h5 tag? Commented Jul 28, 2016 at 7:56

3 Answers 3

5

Try this - needs to be run after you have tried to pull your feeds from another website

var feed = $('#alert-feed');   // get your feed
if (!feed.find('h5').length) { // check if it doesn't contain a h5
  feed.addClass('no-alert');   // if it doesn't, add a class
}
Sign up to request clarification or add additional context in comments.

2 Comments

OP wants to add class when there is no h5 inside #alert-feed. I think your current variant doing opposite. It should be if (!feed.find('h5').length) { ... }
I just realised that and was editing as you typed your comment :)
0

If you just wants to hide #alert-feed based on presence or absence of h5 inside it, you can try this code:

if(!$('#alert-feed h5').length) {
    $('#alert-feed').hide();
}

Otherwise if class is needed then:

if(!$('#alert-feed h5').length) {
    $('#alert-feed').addClass('no-alert');
}

2 Comments

this will only find the direct h5 child of the element with id alert-feed?
@EdwardBlack No, it can find h5 to any level inside #alert-feed. For direct h5 we need to write it as $('#alert-feed > h5').
0

you could use the not() function > .not()

see code snippet below. let me know if it helps

$( ".articlelist" ).not( ":has(h5)" ).addClass( "no-alert" );
.no-alert li { color:red;}
.articlelist:not(.no-alert) { color:blue;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="alert-feed">
    <div class="webpart-title">Title</div>
    <div class="webpart-body">
        <div class="contianer">
            <ul class="feedlist">
                <li class="campusfeed">
                    <ul class="articlelist">
                      <li>
                        <a><h5 class="articletitle">h5 is here !</h5></a>
                      </li>
                      
                    </ul>
                     <ul class="articlelist">
                      <li>
                        no h5 here ! 
                      </li>
                      
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</div>

2 Comments

We dont see whats even happening. Add a start button which triggers your code, so we can see the difference.
edited the answer so as it's more clear. but it was as easy as inspecting the element and you would've seen if the code provided by me worked or not

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.