File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes toggle checkbox group when the max limit is reached Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "toggle checkbox group when the max limit is reached " Watch "toggle checkbox group when the max limit is reached " New topic
Author

toggle checkbox group when the max limit is reached

madhavi sastry
Greenhorn

Joined: Oct 28, 2009
Posts: 3
hi
Suppose i have 10 checkboxes and i allow the user to check max of 4 checkboxes.
When the limit is reached i.e. 4, i need to disable all the other checkboxes
and when i uncheck anyone (now only 3 are checked) all the checkboxes should be enabled

Here's the code which i tried
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">


<script type="text/javascript">

function checkboxlimit(checkgroup,max,min){
var checkgroup=checkgroup
var onlycheckgroup;


for (var i=0; i<checkgroup.length; i++){

checkgroup[i].onclick=function(){
var checkedcount=0

for (var i=0; i><checkgroup.length; i++){
checkedcount+=(checkgroup[i].checked)? 1 : 0
alert("checkedcount is "+checkedcount)


if (checkedcount==max){
var a=checkgroup[i].checked;
alert("only checked "+a);
if(a==true){
onlycheckgroup=document.getElementsByName('selectedItems');
alert("onlycheckgroup "+onlycheckgroup);


alert("in for loop");

alert(1);
onlycheckgroup[i].disabled=true;


}
else{
alert(2);
onlycheckgroup[i].disabled=false;
}


}
}
}
}

}


></script>


</head>

<body>


<input type="checkbox" name="selectedItems" id="check" onclick="checkboxlimit(selectedItems,2)";/> <cut value="John"/> <input type="checkbox" name="selectedItems" id="check" onclick="checkboxlimit(selectedItems,2)";/> <cut value="Smith"/> <input type="checkbox" name="selectedItems" id="check" onclick="checkboxlimit(selectedItems,2)";/> <cut value="Peter"/> <input type="checkbox" name="selectedItems" id="check" onclick="checkboxlimit(selectedItems,2)";/> <cut value="Angel"/>




</form>
</body>

</html>


please help
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Hi Madhvi welcome to javaranch

please Use Code Tags when you post a source code. You can edit your message using button and then add code tags to it and also indent the code properly. Also what happens when you run this code??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

Without the code tags, it's unlikely most people will bother to try and read your post.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
Consider Paul's rocket mass heater.
 
subject: toggle checkbox group when the max limit is reached
 
Similar Threads
save checked value in checkbox to database
Need Help with binding click events to table rows
pass a variable in the URl
Object does not support property error!
Check All button for all spring check boxes array