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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:47:41+00:00 2026-06-11T14:47:41+00:00

While I’m trying to work out the length of an array : $(‘input:checkbox[name=promoOn[]]’).click( function(){

  • 0

While I’m trying to work out the length of an array :

$('input:checkbox[name="promoOn[]"]').click( function(){
  var attributes = [];
  $('input:checkbox[name="promoOn[]"]:checked').each(function(index) { 
    attributes.push($(this).val());
    $('#promo-on-total').html(attributes.length);
  });       
});

For some reason the array.length does not return to 0?
Here are the results:

Checkbox 1, checked = array.length = 1
Checkbox 2, checked = array.length = 2
Checkbox 2, unchecked = array.length = 2
Checkbox 1, unchecked = array.length = 1 (not 0???)

Any ideas what may be wrong?

  • 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-06-11T14:47:43+00:00Added an answer on June 11, 2026 at 2:47 pm

    The problem is that you are running through all checked checkboxes and updating the length value in the promo-on-totaldiv there, but once you uncheck all checkboxes the loop isn’t executed anymore (so the value isn’t updated).

    This will give you what you expect I think:

    $('input:checkbox[name="promoOn[]"]').click( function(){
      var checkedValues = [];
      $('input:checkbox[name="promoOn[]"]:checked').each(function(index) {
        checkedValues.push($(this).val());
      });
    
      $('#promo-on-total').html(checkedValues.length);        
    });
    

    DEMO

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

Sidebar

Related Questions

While I'm trying to handle WARNING messages, I'm getting error for global variable/array (on
While trying to build the mysql2 gem with ruby 1.9.2-p320 on Fedora 16, I
While trying to call an EJB made using NETbeans (using jdk1.7) from a client
While database design I used a field name deleted and use it as Boolean.
While trying to install a program from source I found that it installed libraries
While working in a project written by some one else ,there's a Function which
While trying to run my application I'm getting this error. Sys.WebForms.PageRequestManagerServerErrorException:500 How can i
While trying to start local Google App Engine (v1.3.8) Server on my Fedora 14
While trying to start Apache Tomcat from eclipse, I encounter the following error: 18
While i click the submit button the form will look like below with error

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.