madhu28 0 Newbie Poster

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

function checkboxlimit(checkgroup,max,min){
	var checkgroup=checkgroup
	var limit=limit
	
	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){
			if(document.getElementById('check').checked==true)
			alert(1);
  			document.getElementById('check').disabled=true;

  			}
 			
 			else{
 				alert(2);
 				document.getElementById('check').disabled=false;
 			}
 			
			if(checkedcount>=min)
			{
				document.getElementById('compare').disabled=false;
			}
			else
			{
				document.getElementById('compare').disabled=true;
			}
		}
	}
	}
}

<c:forEach items="${productCommand.productList}" var="prodvar">

<table>
<tr>
	<td>
		<input type="checkbox" name="selectedItems" value="${prodvar.productCode}" id="check"
		 onclick="checkboxlimit(selectedItems,4,2)";/>
		
		
		<c:out value="${prodvar.productCode}"/>
		<c:out value="${prodvar.productDesc}"/>
		<c:out value="${prodvar.APR}"/>
		
	</td>
	
</tr>
</table>
</c:forEach> 

<input type="submit" name="Submit" value="Compare" id="compare" />

</form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.