0
I have the following pure Javascript code (I don’t want to use jQuery here):
const petSize = document.querySelectorAll('.sizeRadio')
const isChecked = (checkableElement => checkableElement.checked ? true : false )
const fixUnselectedRadio = (radioToCheck, radioToUncheck) =>{
radioToCheck.addEventListener('click', ()=>{
radioToUncheck.checked = false
})
}
let tamArray = petSize.forEach((tamanho, index, petSize)=>{
if(isChecked(tamanho)){
fixUnselectedRadio(tamanho, !tamanho )
}
})
<div class="tamanho mt-1 mb-2">
<div class="form-check">
<input class="sizeRadio form-check-input" type="radio" id="gigante" name="gigante" value="gigante" /> </input>
<label class="form-check-label" for="gigante">Gigante (Acima de 70cm - 45 a 60Kg)</label>
</div>
<div class="form-check">
<input class="sizeRadio form-check-input" type="radio" id="grande" name="grande" value="grande" /> </input>
<label class="form-check-label" for="gigante">Grande (De 50 a 69cm - 25 a 45Kg)</label>
</div>
<div class="form-check">
<input class="sizeRadio form-check-input" type="radio" id="medio" name="medio" value="medio" /> </input>
<label class="form-check-label" for="gigante">Médio (De 36cm a 49cm - 15 a 25kg)</label>
</div>
<div class="form-check">
<input class="sizeRadio form-check-input" type="radio" id="medio" name="medio" value="pequeno" /> </input>
<label class="form-check-label" for="gigante">Pequeno (De 28cm a 35cm - 6 a 15kg)</label>
</div>
<div class="form-check">
<input class="sizeRadio form-check-input" type="radio" id="mini" name="mini" value="mini" /> </input>
<label class="form-check-label" for="gigante">Mini (Abaixo de 28cm - até 6kg)</label>
</div>
</div>
When executing and attempting to select radios, will realize what happens: everyone is active with the exception of the third and fourth radios). I need only one to stay active at a time but I couldn’t solve it that way and I couldn’t find any online solution!
How you implemented the function
isChecked? Put the implementation into the body of snippet of the code so that we can execute it.– Luiz Felipe
ah sorry, I didn’t realize I forgot
– Vitor Couto
All right, I updated
– Vitor Couto
Don’t need JS to do this, just give the same
namefor theinputof the kindradio, thus creating a group. In each group, automatically only one input can be active at a time.– Andre
Man, I didn’t think of it :what simple!
– Vitor Couto