I have two lists of checkboxes. First set is for agent and second for function. Now I want that the number of selected function must not exceeded the number of selected agent.
function doAction() {
var nbreFonction = $(".fonction :selected").length;
var max = nbreFonction;
var agent = 0;
var checkboxes = document.getElementsByClassName("agent");
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes.item(i).checked == true) {
agent++
if (agent > max) {
checkboxes.item(i).checked = false;
window.alert('Limite atteinte !');
}
}
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="agent" onClick="doAction()"> Agent 1
<br>
<input type="checkbox" class="agent" onClick="doAction()"> Agent 2
<br>
<input type="checkbox" class="agent" onClick="doAction()"> Agent 3
<br>
<input type="checkbox" class="agent" onClick="doAction()"> Agent 4
<br>
<input type="checkbox" class="agent" onClick="doAction()"> Agent 5
<hr>
<input type="checkbox" class="fonction" onClick="doAction()"> Fonction 1
<br>
<input type="checkbox" class="fonction" onClick="doAction()"> Fonction 2
<br>
<input type="checkbox" class="fonction" onClick="doAction()"> Fonction 3
<br>
<input type="checkbox" class="fonction" onClick="doAction()"> Fonction 4
<br>
<input type="checkbox" class="fonction" onClick="doAction()"> Fonction 5