Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7691661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:40:34+00:00 2026-05-31T20:40:34+00:00

Is there a way to add a class through jquery to all divs containing

  • 0

Is there a way to add a class through jquery to all divs containing ul’s with only one list element (like div.filter_7). With jQuery i need to go through every div.list-container and count the list elements. If the list elements are lower than 2 then jquery should append a class.

<div class="filter_group">
<div class="filter-holder filter_1">
<h4>Test</h4>
<div class="list-container">
    <ul class="list filter">
        <li class="list_id1">list_id1</li>
        <li class="list_id2">list_id2</li>
        <li class="list_id3">list_id3</li>
        <li class="list_id4">list_id4</li>
        <li class="list_id5">list_id5</li>
    </ul>
</div>
</div>
<div class="filter-holder filter_2">
<h4>Test</h4>
<div class="list-container">
    <ul class="list filter">
        <li class="list_id11">list_id11</li>
        <li class="list_id22">list_id22</li>
        <li class="list_id33">list_id33</li>
        <li class="list_id44">list_id44</li>
        <li class="list_id55">list_id55</li>
    </ul>
</div>
</div>
<div class="filter-holder filter_6">
<h4>Test</h4>
<div class="list-container">
    <ul class="list filter">
        <li class="list_id111">list_id111</li>
        <li class="list_id222">list_id222</li>
        <li class="list_id555">list_id555</li>
    </ul>
</div>
</div>
<div class="filter-holder filter_7">
<h4>Test</h4>
<div class="list-container">
    <ul class="list filter">
        <li class="list_id1111">list_id1111</li>
        <li class="list_id2221">list_id2222</li>
    </ul>
</div>
</div>

​

Here is what i’ve done so far with jquery, but it seems wrong:

    var total = 0;
$(document).ready(function() {
    $('.list-container').each(function(idx, item){
      total = $(".list-container>ul").size();
          if (6 >= total) {
           $('.list-container').parent('div').addClass('noshow');
    } else {
    alert(total);
    }              
    });
});

​

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:40:36+00:00Added an answer on May 31, 2026 at 8:40 pm

    You don’t need to filter or each…

    You can use jQuery.has() in conjunction with :only-child. As can be seen in this jsFiddle demo and code below.

    jQuery("div.list-container").has("li:only-child").addClass("noshow");
    

    It’s WAY cleaner to read than using filter. You can modify the has a bit to make it more specific.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a couple divs like so: <div>1</div> <div>2</div all the way through to
Is there a way, through jQuery, to assign an active class to an area
Is there a way to add a mouseover/mouseout css class change to ALL buttons
Is there any way to add a field to a class at runtime (
Is there a way to add new methods to a class, without modifying original
Is there a way in any browser to add/remove class names? For example, if
Is there any easy way to add a using statement to every class I
Is there any way I can add a static extension method to a class.
is there a way to add an existing classic ASP webapp into a solution
Is there a way to add some custom font on a website without using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.