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 6051953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:52:27+00:00 2026-05-23T07:52:27+00:00

I am attempting to add a class to a span, if a set of

  • 0

I am attempting to add a class to a span, if a set of li’s have a specific class applied to them.

li . complete

li

li . complete

li

li . complete

etc…

span # done

I was attempting to add a class of “active” to the span if 4 of the li’s have the class “complete”
I’ve tried the following, and I can get the click event to work but not the if statement, and is this even possible or efficiient?

$(function(){
$("li").click(function(){
   $(this).addClass("complete");
});
if($("li").hasClass("complete")){
   $("#done").addClass("active");
};
});

Ive been digging for a way to maybe count the li.complete’s withing the dom then addClass against it but haven’t had much luck

Thanks

  • 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-23T07:52:27+00:00Added an answer on May 23, 2026 at 7:52 am

    Your current code won’t has two problems: you’re running

    if($("li").hasClass("complete")){
       $("#done").addClass("active");
    };
    

    immediately after binding the click() handler, not after the button has actually been clicked. Additionally, you’re not counting the number of lis that have been made complete.

    One option is this, counting the number of li‘s with the class complete each time you handle the click:

    $(function(){
    $("li").click(function(){
       $(this).addClass("complete");
    
       if ($("li.complete").length >= 4) {
           $("#done").addClass("active");
       }
    });
    });
    

    You could also keep a running count as you go…

    $(function(){
    var activeCount = 0;
    $("li").click(function(){
       $(this).addClass("complete");
    
       activeCount = activeCount + 1;
    
       if (activeCount >= 4) {
           $("#done").addClass("active");
       }
    });
    });
    

    but if you ever want to be able to unselect items, this would make your code more complicated.

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

Sidebar

Related Questions

I have set up the following: Database class ($db) Pagination class ($paginator) I am
I am attempting to add some entities that I have created. When I try
I'm attempting to add a method to a grails domain class, e.g. class Item
I'm attempting to reopen the String class in rails and add a bunch more
So, i'm attempting simple card game. I have player class with draw function, and
I'm attempting to add a property in a partial class to an entity framework
I have a Maven project that's building fine, and I'm attempting to add a
I'm attempting to take the different values in a list, add them up and
I am attempting to add validation to my application. I have some rules I
I have a xml based Relative Layout and was attempting to add a small

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.