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

  • Home
  • SEARCH
  • 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 6766095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:47:22+00:00 2026-05-26T14:47:22+00:00

Here’s the deal. The task is to write a function which should be able

  • 0

Here’s the deal. The task is to write a function which should be able determine the number of checkboxes checked for each question and prompt a user if more than 3 answers were selected.
I have a total of 8 questions and each question has 4 to 8 answers, in the checkbox format.
This is what I came up with:

function countChecks(){
   var m = 0;
   var n = 0;
 chk = document.getElementsByName("DSelectionID");

  for(var i=0; i<myitems.length i=""></myitems.length>
   var value = myItems[i];

  for(n = 0; n < value.length; n++) {
 if(value[n].checked)  {
  m++;
 }
 }
 return m;
 }

the above function works fine for one question and returns ‘m’ to the main function, which handles it this way:

var check = countchecks();

if (check > 3)
 alert ("more than 3 checkboxes were selected");
   else { 
 //do the thing 
}  

to traverse all the 8 questions this is what I came up with:

 function countChecks(){

   var m = 0;
   var n = 0;

//this captures id’s for the right questions

 chk = document.getElementsByName("DSelectionID");
 chk2 = document.getElementsByName("DSelectionID2");
 chk3 = document.getElementsByName("DSelectionID3");
 chk4 = document.getElementsByName("DSelectionID4");
 chk5 = document.getElementsByName("DSelectionID5");
 chk6 = document.getElementsByName("DSelectionID6");
 chk8 = document.getElementsByName("DSelectionID8");
 chk9 = document.getElementsByName("DSelectionID9");

  var myItems = new Array();

  myItems[0]= chk;
  myItems[1]= chk2;
  myItems[2]= chk3;
  myItems[3]= chk4;
  myItems[4]= chk5;
  myItems[5]= chk6;
  myItems[6]= chk8;
  myItems[7]= chk9;

//loops through all the questions
for(var i=0; i
var value = myItems[i];

//loops through the checkboxes for each question

 for(n = 0; n < value.length; n++)
  {
  if( value[n].checked)
  {
      m++;
     if (m > 3) {
    return false; 
   }
  }
 }
   }
}

and the main body handles it like this:

var check = countChecks() 
if (check == false)
alert ("more than 3 checkboxes were selected");
   else {
//do the thing
  }

It is something very simple I’m missing in the countChecks() function
Any ideas?

  • 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-26T14:47:23+00:00Added an answer on May 26, 2026 at 2:47 pm

    Using jquery would make this pretty trivial

    if ($('#yourform input[type=checkbox]:checked').length() > 3) {
       alert('More than 3 checked');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here a simple question : What do you think of code which use try
Here is my code, which takes two version identifiers in the form 1, 5,
Here's a strange question for you guys, I have a nice sorted list that
Here is a synopsis of my code which is a moderately complex WinForms GUI.
Here's my test function (c#, visual studio 2010): [TestMethod()] public void TestGetRelevantWeeks() { List<sbyte>
Here is another spoj problem that asks how to find the number of distinct
Here is my question. I am having this simple menu. <div id=menu> <ul> <li>
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs

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.