0
I need to identify all checkboxes UNCHECKED to add a class by jquery to them, but whenever I submit the form with 1 or 2 checkbox selected, all other checkboxes deselected end up selected.
//Valido meu form
$('#enviar').on('click', function(e){
e.preventDefault();
//verifico se tem algum checkbox marcado e retorna o ID
if($(".stck:checked").length > 0){
var arr = [];
$(".stck").prop("checked", true).each(function(item){
arr.push($(this).attr("id"));
});
console.log(arr);
return arr;
}
//Se não, pego o ID dos desmarcados
else {
var arr = [];
$(".stck").prop("checked", false).each(function(item){
arr.push($(this).attr("id"));
});
console.log(arr);
return arr;
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="stck" name="sticker" id="A" value="A" />
<input type="checkbox" class="stck" name="sticker" id="B" value="B" />
<input type="checkbox" class="stck" name="sticker" id="C" value="C" />
<button id="enviar">enviar</button>
//---MEU JS
I wish I could identify the non-selected checkboxes to inject a class into them.
What code are you trying to add the class to?
– Sergio
You are checking all other checkboxes when you run
$(".stck").prop("checked", true)
within theif
. That’s why he’s ticking all the boxes when you select one or more options. Take a look at the code I posted below, I believe it will solve your problem.– Alexandre Paiva