-4
In short, I have 10 buttons and I want to choose only 3, if you try to click 4° button appears an Alert stating that exceeded the selected limit "So far so good, my code is working", but if I want to uncheck a selected button to mark another, Alert still works and shouldn’t, here is my error in the code. Can someone help me? Note: here the snippet is not working, my host is, I don’t know why, maybe I didn’t know how to set up.
function changeClass(clicked_id) {
itemIndex = $("#div-megasena button.active-mega").length;
if (itemIndex < 2) {
classe = document.getElementById(clicked_id).className;
if (classe == 'btn btn-outline-success') {
document.getElementById(clicked_id).className = 'btn btn-success active-mega';
} else {
document.getElementById(clicked_id).className = 'btn btn-outline-success';
}
} else {
document.getElementById(clicked_id).className = 'btn btn-outline-success teste';
alert('Limite de numeros selecionados atingidos');
}
};
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<button onclick="changeClass(this.id)" id="01" type="button" class="btn btn-outline-success">01</button>
<button onclick="changeClass(this.id)" id="02" type="button" class="btn btn-outline-success">02</button>
<button onclick="changeClass(this.id)" id="03" type="button" class="btn btn-outline-success">03</button>
<button onclick="changeClass(this.id)" id="04" type="button" class="btn btn-outline-success">04</button>
<button onclick="changeClass(this.id)" id="05" type="button" class="btn btn-outline-success">05</button>
<button onclick="changeClass(this.id)" id="06" type="button" class="btn btn-outline-success">06</button>
<button onclick="changeClass(this.id)" id="07" type="button" class="btn btn-outline-success">07</button>
<button onclick="changeClass(this.id)" id="08" type="button" class="btn btn-outline-success">08</button>
<button onclick="changeClass(this.id)" id="09" type="button" class="btn btn-outline-success">09</button>
<button onclick="changeClass(this.id)" id="10" type="button" class="btn btn-outline-success">10</button>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script
Good afternoon... send your code in full, I couldn’t find the $("#div-megasena button.active-mega")
– Richard Gomes