Solved

Count checkboxes

Posted on 2013-01-23
3
260 Views
Last Modified: 2013-01-24
I have four checkboxes, but not allow none or all to be checked.

<!DOCTYPE html>
<html>
<body>

<form action="">
<input type="checkbox" name="vehicle" value="Bike">I have a bike<br>
<input type="checkbox" name="vehicle" value="Car">I have a car<br>
<input type="checkbox" name="vehicle" value="House">I have a house<br>
<input type="checkbox" name="vehicle" value="Cat">I have a cat <br>
</form>

</body>
</html>
0
Comment
Question by:rgbcof
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 22

Accepted Solution

by:
Mrunal earned 250 total points
ID: 38813246
Here is your solution:

$('#btnSubmit').click(function(){
    alert('Total : ' + $('#CHKContainer input:checkbox').length);
    alert('Checked : ' + $('#CHKContainer input:checkbox:checked').length);
	//your code here    
});

Open in new window



Reference:

http://jsfiddle.net/HfnqG/
0
 
LVL 3

Assisted Solution

by:Khilu
Khilu earned 250 total points
ID: 38813260
Here is detailed solution

http://jsfiddle.net/rRZfn/1/

var countChecked = function() {
var n = $( "input:checked" ).length;
    if(n==0)
    {
        alert("Please select least one check box");
    }
    
    if( $( "input:checkbox").length== n)
    {
        alert("Can not select all check box")
    }

};

countChecked();

$( "input[type=checkbox]" ).on( "click", countChecked );

Open in new window

0
 
LVL 10

Expert Comment

by:Monica P
ID: 38813299
var formobj = document.forms[0];

var counter = 0;
Try to findout selected value like this

for (var j = 0; j < formobj.elements.length; j++)
{
    if (formobj.elements[j].type == "checkbox")
    {
        if (formobj.elements[j].checked)
        {
            counter++;
        }
    }      
}

alert('Total Checked = ' + counter);


With jQuery you can:

$("input:checkbox:checked").length;
alert($('form input[type=checkbox]:checked').size());
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Google Tag Manager - Add Trigger Using Div class 22 54
Link not working 6 41
Can't get js code to execute 8 30
testing a web application for ipad on windows pc 3 34
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question