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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:39:06+00:00 2026-05-24T05:39:06+00:00

Is there a more efficient way to write the following script? All it’s doing

  • 0

Is there a more efficient way to write the following script? All it’s doing is updating the count of an item and then displaying the singular or plural text for that count depending on how many it finds.

The code just seems redundant to say “show this one and hide this one, unless the opposite is true then hide this one and show that one”. Shouldn’t there be a way to pass a boolean into something that should show or hide based on that?

var includedCount = $("#services").find("tbody tr td:nth-child(1) :checkbox:checked").length;
var requiredCount = $("#services").find("tbody tr td:nth-child(2) :checkbox:checked").length;

$("#js-included-num").html(includedCount);
if(includedCount === 1){
    $("#js-included-plural").hide();
    $("#js-included-singular").show();
}else{
    $("#js-included-plural").show();
    $("#js-included-singular").hide();
}

$("#js-required-num").html(requiredCount);
if(requiredCount === 1){
    $("#js-required-plural").hide();
    $("#js-required-singular").show();
}else{
    $("#js-required-plural").show();
    $("#js-required-singular").hide();
}
  • 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-24T05:39:06+00:00Added an answer on May 24, 2026 at 5:39 am

    Put it in a function, something along the lines of:

    function togglePlural(baseSelectorName,count) {
    
        var p = $(baseSelectorName+'-plural'), s = $(baseSelectorName+'-singular');
        (count === 1 ? s : p).show();
        (count === 1 ? p : s).hide();
        // or use toggle()
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a more efficient way to write the following appendChild / nesting code?
Is there are more efficient way than the following for selecting the third parent?
Is there a more efficient way to write this code? Or with less code?
Is there a more efficient/elegant way to write this bit of code: $arr1 =
Is there a more efficient way to convert an HTMLCollection to an Array, other
Is there a more efficient way to clamp real numbers than using if statements
Is there a better way to write the following? $(function(){ $('.start-here-notice').fadeIn(1000).animate({left:-155px}, slow) $('.start-here-notice').animate({left:-165px}, slow).delay(5000).fadeOut(1000);
I'm wondering if there is a more efficient method for replacing colors in a
I was wondering if there was a more efficient (efficient as in simpler/cleaner code)
Is there any time in which a reference type is more efficient than a

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.